首页 > 代码库 > Day04
Day04
一:函数对象:函数是第一类对象,即函数可以当作数据传递
1 可以被引用
2 可以当作参数传递
3 返回值可以是函数
3 可以当作容器类型的元素
‘‘‘
#1
# def foo():
# print(‘from foo‘)
# func=foo
#
# print(foo)
# print(func)
# func()
#2
# def foo():
# print(‘from foo‘)
#
# def bar(func):
# print(func)
# func()
#
# bar(foo)
#3
# def foo():
# print(‘from foo‘)
#
# def bar(func):
# return func
#
# f=bar(foo)
#
# print(f)
#
# f()
#4
# def foo():
# print(‘from foo‘)
# dic={‘func‘:foo}
#
# print(dic[‘func‘])
#
# dic[‘func‘]()
#应用
def select(sql):
print(‘========>select‘)
def insert(sql):
print(‘========>add‘)
def delete(sql):
print(‘=======>delete‘)
def update(sql):
print(‘-=---->update‘)
func_dic={
‘select‘:select,
‘update‘:update,
‘insert‘:insert,
‘delete‘:delete
}
def main():
while True:
sql = input(‘>>: ‘).strip()
if not sql:continue
l = sql.split()
cmd=l[0]
if cmd in func_dic:
func_dic[cmd](l)
main()
# def main():
# sql = input(‘>>: ‘)
# l = sql.split()
# print(l)
# if l[0] == ‘select‘:
# select(l)
# elif l[0] == ‘insert‘:
# insert(l)
# elif l[0] == ‘delete‘:
# delete(l)
# elif l[0] == ‘update‘:
# update(l)
1 函数的嵌套调用
2 函数的嵌套定义
‘‘‘
# def max2(x,y):
# return x if x > y else y
#
#
# def max4(a,b,c,d):
# res1=max2(a,b)
# res2=max2(res1,c)
# res3=max2(res2,d)
# return res3
#
# print(max4(10,99,31,22))
#函数的嵌套定义
def f1():
def f2():
print(‘from f2‘)
def f3():
print(‘from f3‘)
f3()
f2()
# f1()
#三种名称空间
#内置名称空间:随着python解释器的启动而产生
# print(sum)
# print(max)
# print(min)
# print(max([1,2,3]))
# import builtins
# for i in dir(builtins):
# print(i)
#全局名称空间:文件的执行会产生全局名称空间,指的是文件级别定义的名字都会放入改空间
# x=1
# if x ==1 :
# y=2
# import time
#
# name=‘egon‘
#
# def func():
# pass
#
# class Foo:
# pass
# x=1
#
#
# def func():
# money=2000
# x=2
# print(‘func‘)
# print(x)
# print(func)
# func()
# print(money)
# func()
# print(x)
#局部名称空间:调用函数时会产生局部名称空间,只在函数调用时临时绑定,调用结束解绑定
# x=10000
# def func():
# x=1
# def f1():
# pass
‘‘‘
作用域:
1. 全局作用域:内置名称空间,全局名层空间
2. 局部作用:局部名称空间
‘‘‘
#名字的查找顺序:局部名称空间---》全局名层空间---》内置名称空间
# # x=1
# def func():
# # x=2
# # print(x)
# # sum=123123
# print(sum)
# func()
# def func():
# x=2
#
# func()
#
# print(x)
#查看全局作用域内的名字:gloabls()
#查看局局作用域内的名字:locals()
# x=1000
# def func():
# x=2
# print(globals())
#
# print(locals())
# print(globals() is locals())
# x=1000
# def func(y):
# x=2
# print(locals())
# print(globals())
#
# func(1)
#全局作用域:全局有效,在任何位置都能被访问到,除非del删掉,否则会一直存活到文件执行完毕
#局部作用域的名字:局部有效,只能在局部范围调用,只在函数调用时才有效,调用结束就失效
x=1
def f1():
print(x)
def foo():
print(x)
def f(x):
# x=4
def f2():
# x=3
def f3():
# x=2
print(x)
f3()
f2()
f(4)
迭代器
迭代的概念:重复+上一次迭代的结果为下一次迭代的初始值
重复的过程称为迭代,每次重复即一次迭代,
并且每次迭代的结果是下一次迭代的初始值
‘‘‘
# while True: #只满足重复,因而不是迭代
# print(‘====>‘)
#下面才为迭代
# l = [1, 2, 3]
# count = 0
# while count < len(l): # 只满足重复,因而不是迭代
# print(‘====>‘, l[count])
# count += 1
#
# l = (1, 2, 3)
# count = 0
# while count < len(l): # 只满足重复,因而不是迭代
# print(‘====>‘, l[count])
# count += 1
# s=‘hello‘
# count = 0
# while count < len(s):
# print(‘====>‘, s[count])
# count += 1
#
#为什么要有迭代器?对于没有索引的数据类型,必须提供一种不依赖索引的迭代方式
#可迭代的对象:内置__iter__方法的,都是可迭代的对象
# [1,2].__iter__()
# ‘hello‘.__iter__()
# (1,2).__iter__()
#
# {‘a‘:1,‘b‘:2}.__iter__()
# {1,2,3}.__iter__()
#迭代器:执行__iter__方法,得到的结果就是迭代器,迭代器对象有__next__方法
# i=[1,2,3].__iter__()
#
# print(i)
#
# print(i.__next__())
# print(i.__next__())
# print(i.__next__())
# print(i.__next__()) #抛出异常:StopIteration
# i={‘a‘:1,‘b‘:2,‘c‘:3}.__iter__()
# print(i.__next__())
# print(i.__next__())
# print(i.__next__())
# print(i.__next__())
# dic={‘a‘:1,‘b‘:2,‘c‘:3}
# i=dic.__iter__()
# while True:
# try:
# key=i.__next__()
# print(dic[key])
# except StopIteration:
# break
# s=‘hello‘
# print(s.__len__())
#
# print(len(s))
#
# len(s)====== s.__len__()
s={‘a‘,3,2,4}
# s.__iter__() #iter(s)
# i=iter(s)
# print(next(i))
# print(next(i))
# print(next(i))
# print(next(i))
# print(next(i))
#如何判断一个对象是可迭代的对象,还是迭代器对象
from collections import Iterable,Iterator
# ‘abc‘.__iter__()
# ().__iter__()
# [].__iter__()
# {‘a‘:1}.__iter__()
# {1,2}.__iter__()
# f=open(‘a.txt‘,‘w‘)
# f.__iter__()
#下列数据类型都是可迭代的对象
# print(isinstance(‘abc‘,Iterable))
# print(isinstance([],Iterable))
# print(isinstance((),Iterable))
# print(isinstance({‘a‘:1},Iterable))
# print(isinstance({1,2},Iterable))
# print(isinstance(f,Iterable))
#只有文件是迭代器对象
# print(isinstance(‘abc‘,Iterator))
# print(isinstance([],Iterator))
# print(isinstance((),Iterator))
# print(isinstance({‘a‘:1},Iterator))
# print(isinstance({1,2},Iterator))
# print(isinstance(f,Iterator))
‘‘‘
可迭代对象:只有__iter__方法,执行该方法得到的迭代器对象
迭代协议:
对象有__next__
对象有__iter__,对于迭代器对象来说,执行__iter__方法,得到的结果仍然是它本身
‘‘‘
# f1=f.__iter__()
#
# print(f)
# print(f1)
# print(f is f1)
#
# l=[]
# i=l.__iter__()
#
# print(i.__iter__())
# print(i)
# print(l)
dic={‘name‘:‘egon‘,‘age‘:18,‘height‘:‘180‘}
# print(dic.items())
# for k,v in dic.items():
# print(k,v)
# i=iter(dic)
# while True:
# try:
# k=next(i)
# print(k)
# except StopIteration:
# break
# for k in dic: #i=iter(dic) k=next(i)
# print(k)
# print(dic[k])
#
# l=[‘a‘,‘b‘,3,9,10]
# for i in l:
# print(i)
# with open(‘a.txt‘,‘r‘,encoding=‘utf-8‘) as f:
# for line in f:
# print(line)
# print(next(f))
# print(next(f))
# print(next(f))
# print(next(f))
# print(next(f))
# print(next(f))
# print(next(f))
# print(next(f))
‘‘‘
迭代器的优点和缺点
优点:
1.提供了一种不依赖下标的迭代方式
2.就跌迭代器本身来说,更节省内存
缺点:
1. 无法获取迭代器对象的长度
2. 不如序列类型取值灵活,是一次性的,只能往后取值,不能往前退
‘‘‘
# l=[10000,2,3,4,5]
#
# i=iter(l)
#
# print(i)
# print(next(i))
#
# f=open(‘a.txt‘,encoding=‘utf-8‘)
#
# for line in f.readlines():
# print(line)
# print(next(f))
# for line in f:
# print(line)
# l=[10000,2,3,4,5]
#
# i=iter(l)
#
# for item in i:
# print(item)
# print(‘=============================‘)
#
#
# for item in i:
# print(item)
l=[10000,2,3,4,5]
i=enumerate(l)
print(next(i))
print(next(i))
闭包:
1. 定义在内部函数
2. 包含对外部作用域而非全局作用域的引用,
该内部函数就成为闭包函数
‘‘‘
#
# def f1():
# x = 1
# def f2():
# print(x)
#
# return f2
#
# f=f1()
# # print(f)
#
# x=100000000000000000000000000
# f()
#闭包应用:惰性计算
from urllib.request import urlopen
def index(url):
def get():
return urlopen(url).read()
return get
oldboy=index(‘http://crm.oldboyedu.com‘)
# print(oldboy().decode(‘utf-8‘))
# print(oldboy.__closure__[0].cell_contents)
# res=urlopen(‘http://crm.oldboyedu.com‘).read()
#
# print(res.decode(‘utf-8‘))
x=1
# y=2
def f1():
# x=1
y=2
def f2():
print(x,y)
return f2
f=f1()
print(f.__closure__[0].cell_contents)
装饰器:修饰别人的工具,修饰添加功能,工具指的是函数
装饰器本身可以是任何可调用对象,被装饰的对象也可以是任意可调用对象
为什么要用装饰器:
开放封闭原则:对修改是封闭的,对扩展是开放的
装饰器就是为了在不修改被装饰对象的源代码以及调用方式的前提下,为期添加新功能
‘‘‘
# import time
#
# def timmer(func):
# def wrapper(*args,**kwargs):
# start_time=time.time()
# res=func(*args,**kwargs)
# stop_time=time.time()
# print(‘run time is %s‘ %(stop_time-start_time))
# return wrapper
#
# @timmer
# def index():
#
# time.sleep(3)
# print(‘welcome to index‘)
#
# index()
# import time
#
# def timmer(func):
# def wrapper():
# start_time=time.time()
# func() #index()
# stop_time=time.time()
# print(‘run time is %s‘ %(stop_time-start_time))
# return wrapper
#
#
# @timmer #index=timmer(index)
# def index():
# time.sleep(3)
# print(‘welcome to index‘)
#
#
# # f=timmer(index)
# # # print(f)
# # f() #wrapper()---->index()
#
# # index=timmer(index) #index==wrapper
#
# index() #wrapper()----->
#流程分析
# import time
# def timmer(func):
# def wrapper():
# start_time=time.time()
# func()
# stop_time=time.time()
# print(‘run time is %s‘ %(stop_time-start_time))
# return wrapper
#
# @timmer #index=timmer(index)
# def index():
# time.sleep(3)
# print(‘welcome to index‘)
#
#
# index() #wrapper()
# import time
# def timmer(func):
# def wrapper(*args,**kwargs):
# start_time=time.time()
# res=func(*args,**kwargs)
# stop_time=time.time()
# print(‘run time is %s‘ %(stop_time-start_time))
# return res
# return wrapper
#
# @timmer #index=timmer(index)
# def index():
# time.sleep(3)
# print(‘welcome to index‘)
# return 1
#
# @timmer
# def foo(name):
# time.sleep(1)
# print(‘from foo‘)
#
#
# res=index() #wrapper()
# print(res)
#
# res1=foo(‘egon‘) #res1=wrapper(‘egon‘)
# print(res1)
#
#
# def auth(func):
# def wrapper(*args,**kwargs):
# name=input(‘>>: ‘)
# password=input(‘>>: ‘)
# if name == ‘egon‘ and password == ‘123‘:
# print(‘\033[45mlogin successful\033[0m‘)
# res=func(*args,**kwargs)
# return res
# else:
# print(‘\033[45mlogin err\033[0m‘)
# return wrapper
#
#
#
# @auth
# def index():
# print(‘welcome to index page‘)
# @auth
# def home(name):
# print(‘%s welcome to home page‘ %name)
#
# index()
# home(‘egon‘)
#
# login_user={‘user‘:None,‘status‘:False}
# def auth(func):
# def wrapper(*args,**kwargs):
# if login_user[‘user‘] and login_user[‘status‘]:
# res=func(*args,**kwargs)
# return res
# else:
# name=input(‘>>: ‘)
# password=input(‘>>: ‘)
# if name == ‘egon‘ and password == ‘123‘:
# login_user[‘user‘]=‘egon‘
# login_user[‘status‘]=True
# print(‘\033[45mlogin successful\033[0m‘)
# res=func(*args,**kwargs)
# return res
# else:
# print(‘\033[45mlogin err\033[0m‘)
# return wrapper
#
# @auth
# def index():
# print(‘welcome to index page‘)
# @auth
# def home(name):
# print(‘%s welcome to home page‘ %name)
# index()
# home(‘egon‘)
#生成器函数:只要函数体包含yield关键字,该函数就是生成器函数
#生成器就是迭代器
# def foo():
# return 1
# return 2
# return 3
# return 4
#
# res1=foo()
# print(res1)
#
# res2=foo()
# print(res2)
# def foo():
# print(‘first‘)
# yield 1
# print(‘second‘)
# yield 2
# print(‘third‘)
# yield 3
# print(‘fourth‘)
# yield 4
# print(‘fifth‘)
#
# g=foo()
# for i in g:
# print(i)
# print(g)
#
# print(next(g)) #触发迭代器g的执行,进而触发函数的执行
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# def counter(n):
# print(‘start...‘)
# i=0
# while i < n:
# yield i
# i+=1
# print(‘end...‘)
#
#
# g=counter(5)
# print(g)
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
‘‘‘
yield的功能:
1.相当于为函数封装好__iter__和__next__
2.return只能返回一次值,函数就终止了,
而yield能返回多次值,每次返回都会将函数暂停,下一次next会从
上一次暂停的位置继续执行
‘‘‘
#tail -f a.txt | grep ‘python‘
import time
def tail(filepath):
with open(filepath,encoding=‘utf-8‘) as f:
f.seek(0,2)
while True:
line=f.readline().strip()
if line:
yield line
else:
time.sleep(0.2)
# t=tail(‘a.txt‘)
#
# for line in t:
# print(line)
def grep(pattern,lines):
for line in lines:
if pattern in line:
yield line
g=grep(‘python‘,tail(‘a.txt‘))
print(g)
for i in g:
print(i)
Day04