Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # def dec1(fn):
- # def sayspam(*args):
- # print("dec1")
- # fn(*args)
- # return sayspam
- # def dec2(fn):
- # def sayspam(*args):
- # print("dec2")
- # fn(*args)
- # return sayspam
- # @dec2
- # @dec1
- # def useful(a,b):
- # print(a*b)
- # if __name__ == "__main__":
- # useful(2,5)
- #程序退出时执行
- # def onexit(f):
- # import atexit
- # atexit.register(f)
- # return f
- # @onexit
- # def func():
- # print("onexit")
- #单例
- def singleton(cls):
- instances = {}
- def getinstance():
- if cls not in instances:
- instances[cls] = cls()
- return instances[cls]
- return getinstance
- @singleton
- class MyClass:
- def __init__(self):
- print(self)
- #添加参数
- def attrs(**kwds):
- def decorate(f):
- for k in kwds:
- setattr(f, k, kwds[k])
- return f
- return decorate
- @attrs(versionadded="2.2",
- author="Guido van Rossum")
- def mymethod(f):
- print(getattr(mymethod,'versionadded',0))
- print(getattr(mymethod,'author',0))
- print(f)
- if __name__ == "__main__":
- # x = MyClass()
- # y = MyClass()
- mymethod(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement