python的语言类型
模块
from…import…和import …
print('模块foo==>')x = 1def get():print(x)def change():global xx=0print(x)
from test import xfrom test import getfrom test import changeprint(x) # 1get() # 1change() # 0get() # 0 (此时test空间中的x被change改掉了)print(x) # 1 (该x还是demo空间中的x,而不是test中的x)from test import x # (再次导入test空间中的x)print(x) # 0 (x才会改变)
包
init.py文件
foo│ m1.py│ m2.py│ m3.py└─ __init__.pydirection1└─demo.py
# 1. 绝对导入from foo.m1 import f1from foo.m2 import f2from foo.m3 import f3print("init")
# 方式1:import foofoo.f1()# 方式2:from foo import f1f1()
常用模块
https://www.cnblogs.com/linhaifeng/articles/6384466.html#_label3
随机验证码random
# 应用场景:随机验证码def make_code(n):res=""for i in range(n):alpha = chr(random.randint(97, 122))number = str(random.randint(0, 9))res +=random.choice([alpha, number])return resprint(make_code(8))
sys.argv使用场景
打印进度条
可以使用tqdm模块print('[%-50s]' %'#')"""%s:格式化字符%50s: 规定最小50个字符,不够补空格,默认右对齐%-50s:左对齐\r:从头开始\n:换行"""https://wenku.baidu.com/view/8b03542a497302768e9951e79b89680202d86b4d.html
日志配置文件
logging.basicConfig(# 1、日志输出位置:1、终端 2、文件filename='access.log', # 不指定,默认打印到终端# 2、日志格式format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',# 3、时间格式datefmt='%Y-%m-%d %H:%M:%S %p',# 4、日志级别# critical => 50# error => 40# warning => 30# info => 20# debug => 10level=30,)
