Home  >  Article  >  Backend Development  >  The difference between python modules and classes

The difference between python modules and classes

(*-*)浩
(*-*)浩Original
2019-06-28 15:46:343548browse

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

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!

Statement:
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