您的位置:首页 > 编程语言 > Python开发

2.7 学python 装饰器2 生成器 迭代器 模块time random

2018-02-07 21:33 771 查看
1.装饰器

#装饰器加参数
def logger(flag):
def show_time(f):
def inner(*x,**y):
start =time.time()
f(*x,**y)
end=time.time()
print('spend %d s'%(end-start))
if flag==True:
log()
return inner
return show_time

@logger(True) #@做logger(True) 返回的东西,即 show_time()
def add(*a,**b):
sum=0
for i in a:
sum+=i
print(sum)

#类装饰器 等到类讲完


2.生成器

#基础一:列表生成式
[x*2 for x in range(10)]#[0,2,4,6,8,10,12,14,16,18]
def f(x):
return x**3
[f(i) for i in range(3)]#[0,1,8]
#生成器
s=(x*2 for x in range(10))
print(s)#<generator object <genexpr> at 0x7f8cf1f106d0>
'''
生成器 里面没有东西,不占地方;列表里面有,占地方
生成器,像一个厨师,现用先做,
'''
# 内置函数next 访问
print(next(s))#0 等价与s.__next__() in python2
print(next(s))#2
print(next(s))#
>>> next(s)
18
>>> next(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
#遍历访问
s=(x*2 for x in range(10))
for i in s:
print(i)
next(s)#StopIteration
# 生成器函数
def foo():
print('OK')
yield 1
print('Ok2')
yield 2

#yield 相当于一个 return
#yield 是一个暂停 的标记
#yield 是一个变量
>>> g=foo()
>>> x=next(g)
OK
>>> x
1
>>> next(g)
OK2
2
>>> foo()
<generator object foo at 0x7f8cf1f10620>

# send()方法
>>> x= foo()
>>> a=x.send()  #必须有一个参数
TypeError: send() takes exactly one argument (0 given)
>>> a=x.send('asdf') #第一次进入,不能有非None参数
TypeError: can’t send non-None value to a just-started generator
>>> next(x)
OK
1
>>> x.send('sdaf') #传的参数可以没有用
Ok2
2
>>> y=foo()
>>> y.send(None) #第一次可以传 None当作参数
OK
1
# send('这个参数') 传递的位置
def bar():
print('come in')
count=yield 1
print(count)
yield 2

>>> g=bar()
>>> g.send(None)
come in
1
>>> g.send('这个参数')
这个参数
2


3 . 可迭代对象

for a in XXX: # XXX 应该是可迭代对象
pass
#什么是可迭代对象 对象有 __iter__() 方法的
l=[1,2]#列表
l.__iter__()

t=(1,3)#元组
t.__iter__()

d={name:'yanga11ang'}
d.__iter__()

#for 封装的内容
for i in g:
g=iter(g) #1调用iter返回一个迭代器对象
while True: #2不断next
try:
x=next(g)
yield x
except StopIteration as e:     #3异常处理
print('Generator return value :',e.value)
break


4.迭代器

#生成器都是迭代器
l=[1,2,3]
l.__iter()__# 一般不用
d=iter(l) #d是一个迭代器
#迭代器满足 1有iter方法 2有next方法
# 判断类型
from collections import Iterator Iterable
isinstance([1,2],list) #判断前面的数据类型和后面的是否匹配


4.time模块

import time
Functions:
time()  return current time in seconds since the Epoch as a float#时间戳
clock()  return CPU time since process start as a float
sleep()  delay for a number of seconds given as a floa  t
gmtime()  convert seconds since Epoch to UTC tuple  #结构化时间
localtime()  convert seconds since Epoch to local time tuple
asctime()  convert time tuple to string
ctime()  convert time in seconds to string
mktime()  convert local time tuple to seconds since Epoch
strftime()  convert time tuple to string according to format specification #help 里面有
strptime()  parse string to time tuple according to format specification
tzset()  change the local timezone

import datetime
datetime.datetime.now()


5.random 模块

import random
random.random() #(0-1)
random.randint(1,8) #[1,8]
random.choice('hello')#随机选
random.shuffle()
random.sample([1,2,3],2) #随机选多个
random.randrange(1,3) #[1,3)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: