本节内容python
1.协程函数(本次先嵌入概念 了解)mysql
#yield:
#1:把函数的执行结果封装好__iter__和__next__,即获得一个迭代器
#2:与return功能相似,均可以返回值,但不一样的是,return只能
#返回一次值,而yield能够返回屡次值
#3:函数暂停与再继续运行的状态是有yield保存
# def func(count):
# print('start')
# while True:
# yield count
# count+=1
#
# g=func(10)
# # print(g)
# print(next(g))
#
# print(next(g))sql
2.递归oracle
#递归调用:在调用一个函数的过程当中,直接或间接地调用了函数自己app
#直接 def func(): print('from func') func() func() #间接 def foo(): print('from foo') bar() def bar(): print('from bar') foo() foo() age(5)=age(4)+2 age(4)=age(3)+2 age(3)=age(2)+2 age(2)=age(1)+2 age(1)=18 age(n)=age(n-1)+2 #n>1 age(1)=18 #n=1 def age(n): if n == 1: return 18 return age(n-1)+2 print(age(5)) #递归的执行分为两个阶段: #1 递推 #2 回溯 l =[1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14, 15,[16,[17,]],19]]]]]]] def search(l): for item in l: if type(item) is list: search(item) else: print(item) search(l)
3 二分法ide
#二分法 l = [1,2,5,7,10,31,44,47,56,99,102,130,240] def binary_search(l,num): print(l) #[10, 31] if len(l) > 1: mid_index=len(l)//2 #1 if num > l[mid_index]: #in the right l=l[mid_index:] #l=[31] binary_search(l,num) elif num < l[mid_index]: #in the left l=l[:mid_index] binary_search(l,num) else: print('find it') else: if l[0] == num: print('find it') else: print('not exist') return binary_search(l,32)
4 import 语句函数
#导入模块干了哪些事: #1 执行源文件 #2 以一个源文件的全局名称空间 #3 在当前位置拿到一个模块名,指向2建立的名称空间 import spam money=100000000000 def read1(): print('from test') # print(spam.money) # print(spam.read1) # spam.read1() # spam.read2() spam.change() print(money) spam.read1() import spam as s1 print(s1.money) sql_type=input('sql_type: ') if sql_type == 'mysql': import mysql as sql elif sql_type == 'oracle': import oracle as sql sql.sqlparse() import sys print(sys) import spam print(spam)
5 from... import语句spa
#优势:使用源文件内的名字时无需加前缀,使用方便 #缺点:容易与当前文件的名称空间内的名字混淆 # from spam import money,read1,read2,change # money=0 # print(money) # print(read1) # # read1() # def read1():print('ok') # read2() # # money=10 # change() # print(money) # from spam import money as m # # print(m) from spam import * # print(_money) # read1() # print(read2) print(money) print(x) print(read1)
6 模块搜索路径code
import time import importlib import spam time.sleep(20) # import spam # print(spam.money) importlib.reload(spam) print(spam.money) import sys print('time' in sys.modules) import time print('time' in sys.modules) import sys import sys #结论: #注意:自定义的模块名必定不要与python自带的模块名重名 #内存中--》内置模块————》sys.path import sys # print(sys.path) sys.path.insert(0,r'C:\Users\Administrator\PycharmProjects\python18期周末班\day5\模块\模块的搜索路径\aaa') import spam
7 区分python文件两种用途协程
import os,sys x=1 def func1(): print('from m1') def func2(): print('from m2') def func3(): print('from m3') # print(__name__) #文件当作脚本运行时__name__等于__main__ #文件当作模块被加载运行时__name__等于模块名 if __name__ == '__main__': #当作脚本使用 func1() func2() func3() 看成模块使用 import m1 # m1.func2() m1.func3()
8 包的导入
9 绝对导入与相对导入(只是在包里面模块没有这个概念)
绝对导入指 的是包的顶级是根目录
#实例 用法 from 包名.包名.文件名 import 函数或者变量名 from atm.bin.core import get(这里get也能够写成* 是指对应文件全部能够调用的名字)
缺点:当更名字的时候 须要修改相关的文件
相对导入
#相对导入 用法 from .bin.core import get #点表明当前目录 from ..core import get 那么若是把当前包移到其余目录下该如何应用 某个包下的功能呢? 这里咱们介入sys import sys sys.path.append(r'要导入的文件路径')
10 软件开发规范
11 logging模块的使用