The difference between python modules and classes

(*-*)浩
Release: 2019-07-09 10:18:44
Original
3552 people have browsed it

In python, classes can provide namespaces below the module level.

The difference between python modules and classes

If a module writes many functions, and some functions jointly complete a set of functions, using classes will look clearer and will be better when calling. ide completion has a smaller range of limited prompts. (Recommended learning: Python video tutorial)

Class provides, inheritance, combination, multiple instances, customization through inheritance, operator overloading (that is, the double underline method __del__ __call__ __str__ __iter__ of the class. .......).

Both classes and modules can establish namespace trees. The functions in modules are called functions, and the functions in classes are generally called methods.

The methods of modules in python can also be inherited and customized like classes.

The following are two files.

modulea.py

# coding=utf8

x = 1

def fun1():
    print 'modulea',x

def fun2():
    print 'modulea',x*2
Copy after login

moduleb.py

# coding=utf8

import modulea

print modulea.__dict__
modulea.fun1()
modulea.fun2()

def fun1():
    print 'moduleb',modulea.x*10

modulea.x = 11
modulea.fun1 = fun1

print '替换后'


modulea.fun1()
modulea.fun2()
Copy after login

modulea’s fun1 function has been changed, similar to class inheritance.

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between python modules and classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!