zoukankan      html  css  js  c++  java
  • python装饰器

    #通过引用全局变量来控制账号登陆
    flag = False def login(func): def inner(*args,**kwargs): global flag if flag: ret = func(*args, **kwargs) return ret else: username = input('user:') password = input('pswd:') if username == 'ryan' and password == '123456': flag = True ret = func(*args, **kwargs) return ret return inner @login def say_hi(): print('hello world') @login def say_bye(): print('goodbye my friends') say_hi() say_bye()

     计时器:

    from time import time
    from time import sleep
    def timmer(func):
        def inner(*args,**kwargs):
            start = time()
            ret = func(*args,**kwargs)
            end = time()
            print(end - start)
            return ret
        return inner
    
    
    @timmer
    def func_1(words):
        sleep(1)
        print(words)
    
    
    @timmer
    def func_2(words):
        sleep(2)
        print(words)
    
    
    func_1('你好呀')
    func_2('你是大坏蛋')
  • 相关阅读:
    poj 1562 Oil Deposits
    poj 1650 Integer Approximation
    snmp4j 编程
    ubuntu 13.04 163源(亲测可用)
    c语言中static 用法总结(转)
    Spring入门
    Hibernate入门
    Struts2入门教程
    素数距离问题
    ASCII码排序
  • 原文地址:https://www.cnblogs.com/thanos-ryan/p/13368974.html
Copyright © 2011-2022 走看看