Learning records
Date: morning of September 7, 2021
modular
What is a module:
Any file with. py suffix can be called a module
What can be included in the module
1. Variable
2. Functions
3. Object oriented (class - > object)
4. Executable code
What are the benefits of using modules
Convenient management and easy maintenance
Reduce complexity
PI = 3.14 def get_area(r): return PI * r ** 2 class Student(): def __init__(self,name,age): self.name = name self.age = age def show_info(self): print('name:{0},age:{1}'.format(self.name,self.age)) print(PI) print('The area of a circle with radius 2 is:{0}'.format(get_area(2))) stu = Student('James',20) stu.show_info()
How to use the module:
Custom module
Import module:
1. import module name 1, module name 2....
How to use after import?
Module name. Function name (parameter)
Module name. Class
Module name. Variable
2. Import relevant data in the module
from module, import variable, function, class
How to use after import?
Can be used directly
#1. How to import modules import random result = random.randint(1,6) print(result) #2. How to import relevant data in the module from random import randint result = randint(1,6) print(result)
Customize a module
Realize the four operations of Mathematics
Addition, subtraction, multiplication and division of two numbers
def add(a,b): #Addition operation #return: the sum of two numbers return a + b def sub(a,b): return a - b def mul(a,b): return a * b def div(a,b): return a / b if __name__ == '__main__': a = 10 b = 2 print('And:{0}'.format(add(a,b))) print('Poor:{0}'.format(sub(a,b))) print('Product:{0}'.format(mul(a,b))) print('Business:{0}'.format(div(a,b))) #__main__ print(__name__)
Import custom module
1. import module
Problem: when importing a module, the module code will be executed again
Solution:
In a custom module:
New control code:
if name == 'main':
Test code execution
2. from module import function...
import MyMath x = 10 y = 20 print('And:{0}'.format(MyMath.add(x, y))) from MyMath import add print('And are:{0}'.format(add(x,y)))
__ all__ method
When using a custom module:
1. import module
2. from module import variables, classes, functions....
Problem: if you want to use this scheme to import all the functions in the module, it is troublesome to fill in one by one
Solution:
from module import*
*All functions in the default import module
If you manually add global variables to a module__ all__ = []
Add global variables manually__ all__ After that, the from module import * will no longer import all functions by default,
It's import__ all__ Functions in the list (not recommended in Python 3)
__all__ = ['add','sub','mul','div'] def add(a,b): #Addition operation #return: the sum of two numbers return a + b def sub(a,b): return a - b def mul(a,b): return a * b def div(a,b): return a / b if __name__ == '__main__': a = 10 b = 2 print('And:{0}'.format(add(a,b))) print('Poor:{0}'.format(sub(a,b))) print('Product:{0}'.format(mul(a,b))) print('Business:{0}'.format(div(a,b))) #__main__ print(__name__)
from MyMath import * result = add(10,20) print(result) result = sub(10,20) print(result) result = mul(10,20) print(result) result = div(10,20) print(result)
package
Concept of package:
It can be understood as a folder, provided that the file contains a__ init__.py
Function of package:
1. Classify the modules to facilitate sorting
2. Prevent module name conflicts
The package name in the module will change
New name:
Package name. Module name
Mymath
package1.MyMath
How to use the modules in the package:
1. import module
2. from module, import variable, function, class
# import MyMath # result = MyMath.add(10,20) # import package1.MyMath # result = package1.MyMath.add(10,20) # print(result) from package1.MyMath import * result = add(10,20) print(result) result = sub(20,10) print(result)
Package__ init__. Use of PY
package
init.py
In class__init__Is the initialization method In package__init__.py Is the initialization module initialization When using a module in a package for the first time,__init__.py The modules found in are executed once
What can be stored in init.py?
It can store the same code as ordinary modules
Variables, classes, functions... All ok
I usually write some auxiliary codes:
Make it easier for you to use the module
In the test file import package In the package__init__.py Import in import modular This method is equivalent to: using in the test file import package.Module name ------------------------- In the test file from package import * stay__init__.py in from .modular import * This method is equivalent to: using in the test file from package.Module name import *
# import package1.MyMath # import package1 # # import package1.MyMath # result = package1.MyMath.add(10,20) # print(result) # from package1.MyMath import * from package1 import * result = add(10,20) print(result)
Contents in init.py
# print('I'm from package1__ init__ Method ') # import MyMath #Import class content in module from .MyMath import *