Program module package python

python program is composed of modules, a python file is a module, which is generally by the code, function, or classes. Creating baiduHq.py module (file), write variables, functions, classes in this module to illustrate in a module, output variables, function calls, call the class, see the source code:

#coding:utf-8
hello='hello world'
def add(a,b):
return a+b
class index(object):
def __init__(self):
pass
def add(self,a,b):
return a+b

Note: the source can be obtained from the above and the screenshot, baiduHp.py module defined in the hello variable, the Add () function with the class index, in a module, the functions may be written in N, N classes, which mainly depends on the preparation of organization and people need to code. In a module, how to call a function, a class?

__name__ __ == IF '__ main__': 
Print (hello)
Print (the Add (2,3))
Demo = index ()
Print (demo.add (3,3))
Note: See above screenshots, hello output variable is simple, add () function because parameters, time parameters have to add the call, required to instantiate class index, where the method invocation before instantiation. Used herein to __name __ == '__ main__', which is the entrance python program, can be understood as the main java

Guess you like

Origin www.cnblogs.com/confidence-pearl/p/10979254.html