Module
Introduction to the module
- Modular
- Modularization: break a complete program into a small module
- The advantages of modularity:
1. Easy to develop and easy to maintain
2. Modules can be reused
- A py file in python is a module, and
an external module is introduced in a module
You can introduce the same module multiple times, but the module will only be executed once
The introduction of the module
- The first type: import module name (the module name is the python file name)
text
print('这是我的第一个模块')
Test file
import text
print(text)# <一个模块的名字和指向的路径>
- The second type: import module name as module alias
text
print(__name__)
test
import text as text_m
print(text_m)# <一个模块的名字和指向的路径>
# __name__:在每一个模块内部都有这个__name__我们可以通过这个获取模块的名字
# 如果这个模块直接运行,那么这个__name__默认的字符串__main__
- The third type: from module name import variable, variable, variable...
test:
from text import Person,text1,text2
#print(text)
p1 = Person()
print(p1)
text1()
text2()
Module
a = 1
b = 2
# 在模块中定义函数
def text1():
print('text1')
def text2():
print('text2')
# 在模块中定义类
class Person:
def __init__(self):
self.name = '葫芦娃'
p = Person()
print(p.name)
- Fourth: from module name import *
test
from text import *
text1()
text2()
Module
# 在模块中定义变量
a = 1
b = 2
# 在模块中定义函数
def text1():
print('text1')
def text2():
print('text2')
# 在模块中定义类
class Person:
def __init__(self):
self.name = '葫芦娃'
p = Person()
print(p.name)
Test Results
葫芦娃
text1
text2
- Fifth: from module name import variable as alias
def text1():
print('主模块中的text1')
from text import text1 as new_text1
text1()
new_text1()
# 在模块中定义变量
a = 1
b = 2
# 在模块中定义函数
def text1():
print('text1')
def text2():
print('text2')
# 在模块中定义类
class Person:
def __init__(self):
self.name = '葫芦娃'
p = Person()
print(p.name)
葫芦娃
主模块中的text1
text1
Module usage
test
# 访问模块中的变量的语法:
# 模块名.变量
import text
print(text.a,text.b)
# 访问模块中的函数的语法:
# 模块名.函数
text.text1()
text.text2()
# 访问模块中的类
# 语法:模块名.对象名
p = text.Person()
print(p.name)
Module
# 在模块中定义变量
a = 1
b = 2
# 在模块中定义函数
def text1():
print('text1')
def text2():
print('text2')
# 在模块中定义类
class Person:
def __init__(self):
self.name = '葫芦娃'
p = Person()
print(p.name)
Test Results
葫芦娃
1 2
text1
text2
葫芦娃
# 在模块中定义变量
a = 1
b = 2
c = 3# c是私有的,不希望去修改
# 在模块中定义函数
def text1():
print('text1')
def text2():
print('text2')
# 在模块中定义类
class Person:
def __init__(self):
self.name = '葫芦娃'
p = Person()
# 如果这个文件是主文件则执行下列代码
if __name__ == '__main__':
# 以下是测试代码
print(p.name)
text1()
text2()
葫芦娃
text1
text2
Iterators and generators
Iterator
A way to access elements, you can remember the object of the traversal position, the iterator is also accessed from the first element in the sequence until all the elements are accessed.
lst = [1,2,3,4,5,6,7]
myiter = iter(lst)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
1
2
3
4
5
6
7
lst = [1,2,3,4,5,6,7]
myiter = iter(lst)
for i in myiter:
# 迭代数据结构
print(i)
1
2
3
4
5
6
7
Builder
Using the keyword yield in python is called generator function or generator
- The difference between generators and ordinary functions
What is returned is an iterator function, only used for iteration
def fn():
print(1)
yield 11
print('2')
yield 29
print('kjkjk')
yield 90
yield 78
print(type(fn))
f = fn()
print(type(f))
next(f)
print('返回:',next(f))
print('返回:',next(f))
print('返回:',next(f))
<class 'function'>
<class 'generator'>
1
2
返回: 29
kjkjk
返回: 90
返回: 78