python的语言类型

python语言类型.png

模块

模块.png

from…import…和import …

  1. print('模块foo==>')
  2. x = 1
  3. def get():
  4. print(x)
  5. def change():
  6. global x
  7. x=0
  8. print(x)
  1. from test import x
  2. from test import get
  3. from test import change
  4. print(x) # 1
  5. get() # 1
  6. change() # 0
  7. get() # 0 (此时test空间中的x被change改掉了)
  8. print(x) # 1 (该x还是demo空间中的x,而不是test中的x)
  9. from test import x # (再次导入test空间中的x)
  10. print(x) # 0 (x才会改变)

包.png

init.py文件

  1. foo
  2. m1.py
  3. m2.py
  4. m3.py
  5. └─ __init__.py
  6. direction1
  7. └─demo.py
  1. # 1. 绝对导入
  2. from foo.m1 import f1
  3. from foo.m2 import f2
  4. from foo.m3 import f3
  5. print("init")
  1. # 方式1:
  2. import foo
  3. foo.f1()
  4. # 方式2:
  5. from foo import f1
  6. f1()

常用模块

https://www.cnblogs.com/linhaifeng/articles/6384466.html#_label3
常用模块.png

随机验证码random

  1. # 应用场景:随机验证码
  2. def make_code(n):
  3. res=""
  4. for i in range(n):
  5. alpha = chr(random.randint(97, 122))
  6. number = str(random.randint(0, 9))
  7. res +=random.choice([alpha, number])
  8. return res
  9. print(make_code(8))

sys.argv使用场景

image.png

打印进度条

  1. 可以使用tqdm模块
  2. print('[%-50s]' %'#')
  3. """
  4. %s:格式化字符
  5. %50s: 规定最小50个字符,不够补空格,默认右对齐
  6. %-50s:左对齐
  7. \r:从头开始
  8. \n:换行
  9. """
  10. https://wenku.baidu.com/view/8b03542a497302768e9951e79b89680202d86b4d.html

日志配置文件

  1. logging.basicConfig(
  2. # 1、日志输出位置:1、终端 2、文件
  3. filename='access.log', # 不指定,默认打印到终端
  4. # 2、日志格式
  5. format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
  6. # 3、时间格式
  7. datefmt='%Y-%m-%d %H:%M:%S %p',
  8. # 4、日志级别
  9. # critical => 50
  10. # error => 40
  11. # warning => 30
  12. # info => 20
  13. # debug => 10
  14. level=30,
  15. )