I.. __init__.py file
The original python module has a __init__ in each package. PY file (This file defines the properties and methods of the package) and then some module files and subdirectories, if there are __init__.py in the subdirectory then it is the child of this package. When you import a package as a module (such as importing the Dom from XML ), you actually import its __init__. PY file.
A package is a __init__ with a special file . The directory of the PY. __init__. py The file defines the properties and methods of the package. It can be nothing but a blank file, but it must exist. If __init__. PY does not exist, this directory is just a directory, not a package, it can not be imported or contain other modules and nested package.
In Python, the module (also known as Python) is a separate file to be implemented, whether it is a py file, or a pyc file, or even a C extension DLL file. And for the package, Python uses a folder to implement it, it can be said that a folder is a bag, which contains some py, PYC or DLL files, this way is to get the module poly-Synthesis of the specific implementation
In the process of introducing the package, theinit. PY will run, so if some variables or methods require resident memory, they can be written to the init. py file.
There is also an important variable in __init__.py, called __all__. We sometimes take a trick of "import All", which is this:
Import *
Import then imports the sub-modules and sub-packages that are registered in the __ALL__ list in the package __init__.py file into the current scope. Like what:
#文件 __init__.py
__all__ = ["Module1", "Module2", "SubPackage1", "SubPackage2"]
Second, __init__ function
Parent Class A
1 class A (object):2 def __init__ (self, name):3 self.name=Name 4 "name:", Self.name 5 def getName (self):6 return" + self.name
View Code
- Subclasses do not override __init__, and when a subclass is instantiated, the __init__ of the parent class definition is automatically called
Subclass B Inherits Parent Class A
1 classB (A):2 def getName (self):3 return 'B'+Self.name4 5 if__name__=='__main__':6B=b ('Hello')7Print B.getname ()
View Code
Enter the result:
Name:hellob Hello
2. When subclasses rewrite __init__, the subclass is instantiated, and the __init__ method defined by the parent class is not called
3. Subclasses in order to be able to use or extend the behavior of the parent class, it is best to display the __init__ method calling the parent class
Class Subclass:
def __init__ (self, parameters):
Super (subclass, Self). __init__ (parameter) #执行父类的__init__方法
1 classA (object):2 def __init__(self, name):3Self.name=name4 Print "Name:", Self.name5 defGetName (self):6 return 'A'+Self.name7 8 classB (A):9 def __init__(self, name):TenSuper (B, self).__init__(name) One Print "Hi" ASelf.name =name - defGetName (self): - return 'B'+Self.name the - if __name__=='__main__': -B=b ('Hello') - PrintB.getname ()
View Code
Output Result:
Name:hellohib Hello
Python basics: The role of __init__.py and __init__ functions