In python, classes can provide namespaces below the module level.
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
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()
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!