python-生成器&装饰器&迭代器

生成器:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Vergil Fu

# 只有在调用是才生成数据
# 只记录当前位置
# 只有一个__next__()方法
# [i*2 for i in range(10)]#########列表生成式
# (i*2 for i in range(10))#########列表生成器

import time

'''
def fib(max):############斐波那契函数
    n , a , b = 0 , 0 , 1
    while n < max:
        yield b
        a , b = b ,a + b
        n+=1

f = fib(10)
print(f.__next__())
'''


def rec(obj):
    print("prepare to recive pigs %s" % obj)
    while True:
        pig = yield
        print("把第%s个pig送给了%s" % (pig, obj))


def pro(master):
    e1 = rec("vergil")
    e1.__next__()
    for i in range(1, 99999999999999999999999999999999999999999):
        print("%s做好了第%s个pig" % (Mr_right, i))
        e1.send(i)


pro("danti")

装饰器:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Vergil Fu

#1.函数即变量
#2.高阶函数
#3.嵌套函数
import time
'''
#基础版
def deco(func):
    def wrap(*args,**kwargs):
        start_time = time.time()
        func()
        end_time = time.time()
        print(end_time - start_time)
        print("in the deco")
    return wrap

@deco
def func():
    time.sleep(3)
    print("in the func")
func()

@deco
def hello():
    print("in the hello".center(50, "-"))
    print("hello world!")
hello()


##等同于装饰器
def func1():
    time.sleep(3)
    print("in the func1")

def deco1(func):
    print("the %s is running!"%func)
    return func

deco1(func1)


#进阶版
mysql = {"username1":"vergil","pw1":123456,"job1":"test","balance1":20000,"age1":25,"salary1":22}

def key(func):
    def wrap(*args,**kwargs):
        username = input("username:")
        password = int(input("password:"))
        if username == mysql["username1"] and password == mysql["pw1"]:
            func()
        else:
            print("wrong message!!!")
    return wrap

@key
'''
# def info(*args,**kwargs):
#     info1 = '''
# --------info of {name}--------
# my name is {name}
# I am {age} year-old
# I earn {salary} per month'''.format(name = mysql["username1"],age = mysql["age1"],salary = mysql["salary1"])
#     print(info1)
# info()


#加强版
mysql = {"user_local":"vergil","user_ad":"admin","pw_local":123456,"pw_ad":123456}
def web(author_type):
    def wrapper(func):
        def wrapper_out(*args,**kwargs):
            username = input("username:")
            passwd = int(input("password:"))
            if author_type == "local":
                if username == mysql["user_local"] and passwd == mysql["pw_local"]:
                    print("hello %s"%username)
                    func()
                else:
                    print("invalid input")
            else:
                if username == mysql["user_ad"] and passwd == mysql["pw_ad"]:
                    print("hello %s"%username)
                    func()
                else:
                    print("invalid input")
        return wrapper_out
    return wrapper

@web(author_type = "local")
def local():
    print("welcome local guest!!!".center(50,"-"))

@web(author_type = "administrator")
def admin():
    print("welcome administrator!!!".center(50,"-"))



local()
admin()

迭代器:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Vergil Fu

#迭代器:可以用for循环,可以调用next方法
#生成器一定是迭代器,迭代器不一定是生成器

from collections import Iterator

a = (i for i in range(0,10))
b = [1,2,3]
c = "123"
d = {"a":1,"b":2,"c":3}

print(isinstance(a,Iterator))
print(isinstance(b,Iterator))
print(isinstance(c,Iterator))
print(isinstance(d,Iterator))
print(isinstance(iter(b),Iterator))
print(isinstance(iter(c),Iterator))
print(isinstance(iter(d),Iterator))

it = iter([1,2.3])
while True:
    try:
        x = next(it)
    except StopIteration:
        break

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值