python第四天

一、知识储备
1、*args(接收溢出的位置参数),**kwargs(接收溢出的关键字参数)
2、函数对象:函数可以被当做数据传递
-函数可以当做参数传给另一个函数
-一个函数的返回值也可以第另一个函数(打破函数的层级限制)
3、名称空间与作用域
名称空间
-分三种
内置名称空间:python解释器启动则生效
全局名称空间:执行python文件是生效
局部名称空间:调用函数是,临时生效,函数调用结束则失效
-加载顺序:先内置,再全局,最后有可能产生局部
-查找名字的顺序:先局部,再全局,最后内置
作用域
-分两种
全局作用域:全局存活,全局有效
局部作用域:临时存活,局部有效
强调:作用关系在函数定义阶段就已经固定,与调用位置无关
二、闭包函数

# #闭包函数的定义:定义在函数内部的函数,特点是:包含对外部作用域而不是对全局作用域名字的引用,该函数就称之为闭包函数
# from urllib.request import urlopen
# #函数体内部需要一个变量,有两种解决方案
# #一种是:以参数的形式传入
# def get(url):
# return urlopen(url).read()
# get('http://www.baidu.com')
#
#
# #另外一种就是闭包函数
# def get(url):
# def inner():
# return urlopen(url).read()
# return inner
#
# baidu=get('http://www.baidu.com')
#
# # baidu()

# def get(x,y):
# def inner():
# print(x,y)
# return inner
# baidu=get('a','b')
# print(baidu.__closure__[0].cell_contents)#__closure__(闭包的意思)
# print(baidu.__closure__[1].cell_contents)

三、装饰器
#1、为什么要用装饰器:开放封闭原则,对扩展是开放的,对修改是封闭的
'''
1、为什么要用装饰器:开放性封闭原则,对扩展是开放的,对修改是封闭的
2、什么是装饰器
-用来装饰它人,装饰器本身可以是任意可调用对象,被装饰的对象也可以是任意可调用对象
-遵循的原则:1、不修改被装饰对象的源代码2、不修改被装饰对象的调用方式
目标是:在遵循原则1和2的前提,为被装饰对象添加上新功能

'''


# import time
#
# def timmer(func):
# def inner():
# start_time=time.time()
# func()
# stop_time=time.time()
# print('run time is :['
# '%s]' %(stop_time-start_time))
# return inner
# @timmer
# def index():
# time.sleep(3)
# print('welcome to index page')
# # index=timmer(index)
# index()
四、迭代器
'''
1 什么叫迭代:迭代是一个重复过程,每次重复都是基于上一次的结果来的
2 为什么要用迭代器?
l=['a','b','c']
n=0
while n < len(l):
print(l[n])
n+=1
- 对于序列类型:字符串,列表,元组,可以使用基于索引的迭代取值方式,而对于没有索引的类型,如字典,
集合、文件,这种方式不再适用,于是我们必须找出一种能不依赖于索引的取值方式,这就是迭代器

3 可迭代的对象:只要对象内置有__iter__方法,obj.__iter__
4 迭代器对象:对象既内置有__iter__方法,又内置有__next__,如文件对象
注意:可迭代对象不一定是迭代器对象,而迭代器对象一定是可迭代的对象

'''
#可迭代的对象
# 'hello'.__iter__
# [1,2].__iter__
# (1,2).__iter__
# {'a':1}.__iter__
# {1,2,3}.__iter__
#

#既是可迭代对象,又是迭代器对象
# open('a.txt','w').__iter__
# open('a.txt','w').__next__


# 迭代器对象执行__iter__得到的仍然是它本身
# dic={'a':1,'b':2,'c':3}
# iter_dic=dic.__iter__()
#
# print(iter_dic.__iter__() is iter_dic)



# f=open('a.txt','w')
# print(f is f.__iter__())


#迭代器对象的用处
# dic={'a':1,'b':2,'c':3}
# iter_dic=dic.__iter__()


# print(iter_dic.__next__())
# print(next(iter_dic))
# print(next(iter_dic))
# print(next(iter_dic)) #StopIteration


# with open('a.txt','r') as f:
# print(next(f))
# print(next(f))
# print(next(f))


# l=[1,2,3,4,5]
# iter_l=l.__iter__()
# print(iter_l)
# print(next(iter_l))
# print(next(iter_l))
# print(next(iter_l))

#基于迭代器对象的迭代取值(不依赖索引)
dic={'a':1,'b':2,'c':3}

iter_dic=dic.__iter__()
obj=range(1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)

# list(obj)


# while True:
# try:
# i=next(iter_dic)
# print(i)
# except StopIteration:
# break
#

# for i in dic: #iter_dic=dic.__iter__()
# print(i)


'''
迭代器的优缺点:
- 优点:
提供了一种统一的迭代取值方式,该方式不再依赖于索引
更节省内存

- 缺点:
无法统计长度
一次性的,只能往后走,不能往前退,无法获取指定位置的值
'''



from collections import Iterable,Iterator

print(isinstance('hello',Iterable))
print(isinstance('hello',Iterator))
五、生成器
'''
定义:只要函数内部出现yield关键字,那么再调用该函数,将不会立即执行函数体代码,会到到一个结果
该结果就是生成器对象


'''

#
# def func():
# print('===>first')
# yield 1
# print('===>second')
# yield 2
# print('====>third')
# yield 3
#
#
# g=func()
# print(g)

# 生成器本质就是迭代器
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))


# print(next(func()))
# print(next(func()))
# print(next(func()))




# for i in g:
# print(i)
#
# for i in g:
# print(i)
#
# for i in g:
# print(i)

'''
yield的功能:
- 为我们提供了一种自定义迭代器的方式
- 对比return,可以返回多次值,挂起函数的运行状态

'''

# 自定义功能,可以生成无穷多个值,因为同一时间在内存中只有一个值
# def my_range(start,stop,step=1):
# while start < stop:
# yield start
# start+=step


# g=my_range(1,5,2) #1 3
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
#
# for i in my_range(1,1000000000000000000000000000000000000000000,step=2):
# print(i)




# tail -f access.log | grep '404'
# import time
# def tail(filepath):
# with open(filepath,'rb') as f:
# f.seek(0,2)
# while True:
# line=f.readline()
# if line:
# yield line
# else:
# time.sleep(0.2)
#
# def grep(pattern,lines):
# for line in lines:
# line=line.decode('utf-8')
# if pattern in line:
# yield line
#
# g=grep('404',tail('access.log'))
# for line in g:
# print(line)








# yield的表达式形式的应用
# def eater(name):
# food_list=[]
# print('%s 开动啦' %name)
# while True:
# food=yield food_list #food=‘骨头’
# print('%s 开始吃 %s' %(name,food))
# food_list.append(food)
#
# g=eater('alex')

# g.send(None) #next(g)
# print(g.send('骨头'))
# print(g.send('shi'))



# def f1():
# while True:
# x=yield
# print(x)
#
# g=f1()
# next(g)
# g.send(1)
# g.send(1)
# g.close()
# g.send(1)
# g.send(1)
# g.send(1)
六、面向过程的编程
'''
强调:面向过程编程绝对不是用函数编程那么简单

面向过程的编程思想:核心是过程二字,过程即解决问题的步骤,即先干什么再干什么
基于该思想去编写程序就好比在设计一条流水线,是一种机械式的编程思想

优点:复杂的问题流程化,进而简单化
缺点:可扩展性差


'''
# import os
# g=os.walk(r'C:UsersAdministratorPycharmProjects19期day4a')
# for dirname,_,files in g:
# for file in files:
# abs_file_path=r'%s\%s' %(dirname,file)
# print(abs_file_path)


# grep -rl 'root' /etc
import os


def init(func):
def inner(*args, **kwargs):
g = func(*args, **kwargs)
next(g)
return g

return inner


def search(filepath, target): # 找到一个文件路径就往下个阶段传一次
g = os.walk(filepath)
for dirname, _, files in g:
for file in files:
abs_file_path = r'%s\%s' % (dirname, file)
target.send(abs_file_path)


@init
def opener(target):
while True:
abs_file_path = yield
with open(abs_file_path, 'rb') as f:
target.send((f, abs_file_path))


@init
def cat(target):
while True:
f, abs_file_path = yield
for line in f:
res = target.send((line, abs_file_path))
if res:
break


@init
def grep(pattern, target):
tag = False
pattern = pattern.encode('utf-8')
while True:
line, abs_file_path = yield tag
tag = False
if pattern in line:
target.send(abs_file_path)
tag = True


@init
def printer():
while True:
abs_file_path = yield
print(abs_file_path)


search(r'C:UsersAdministratorPycharmProjects19期day4a', opener(cat(grep('你好', printer()))))
七、三元表达式
# name=input('>>: ')
# if name == 'alex':
# print('SB')
# else:
# print('NB')

# name = input('>>: ')
# print('SB' if name == 'alex' else 'NB')

def my_max(x, y):
return x if x > y else y
八、列表解析与生成器表达式
egg_list = []
for i in range(10):
if i >= 3:
res = 'egg%s' % i
egg_list.append(res)

# print(egg_list)
#
#
# l=['egg%s' %i for i in range(10) if i >= 3]
# print(l)
#
# g=('egg%s' %i for i in range(10) if i >= 3)
# print(next(g))

# for i in ...:
# if ...:
# for i in ...:
# if ...:
# for ...


names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']

names = [name.upper() for name in names if not name.endswith('sb')]
print(names)
九、序列化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# user={'name':'egon','pwd':'123'}
# with open('db.txt','w',encoding='utf-8') as f:
# f.write(str(user))

# with open('db.txt','r',encoding='utf-8') as f:
# data=f.read()
# print(data)


import json

# user={'name':'egon','pwd':'123','age':18}
# with open('db.json','w',encoding='utf-8') as f:
# f.write(json.dumps(user))


# with open('db.json','r',encoding='utf-8') as f:
# data=f.read()
# dic=json.loads(data)
# print(dic['egon'])


user = {'name': 'egon', 'pwd': '123', 'age': 18}
l = [1, 2, 3, 'a']
json.dump(user, open('db1.json', 'w', encoding='utf-8'))



# dic=json.load(open('db1.json','r',encoding='utf-8'))
# print(dic,type(dic),dic['name'])










































































































原文地址:https://www.cnblogs.com/lingmei/p/7650927.html