Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. # def dec1(fn):
  2. # def sayspam(*args):
  3. # print("dec1")
  4. # fn(*args)
  5. # return sayspam
  6.  
  7. # def dec2(fn):
  8. # def sayspam(*args):
  9. # print("dec2")
  10. # fn(*args)
  11. # return sayspam
  12.  
  13. # @dec2
  14. # @dec1
  15. # def useful(a,b):
  16. # print(a*b)
  17.  
  18. # if __name__ == "__main__":
  19. # useful(2,5)
  20.  
  21. #程序退出时执行
  22. # def onexit(f):
  23. # import atexit
  24. # atexit.register(f)
  25. # return f
  26.  
  27.  
  28. # @onexit
  29. # def func():
  30. # print("onexit")
  31.  
  32. #单例
  33. def singleton(cls):
  34. instances = {}
  35. def getinstance():
  36. if cls not in instances:
  37. instances[cls] = cls()
  38. return instances[cls]
  39. return getinstance
  40.  
  41. @singleton
  42. class MyClass:
  43. def __init__(self):
  44. print(self)
  45.  
  46.  
  47. #添加参数
  48. def attrs(**kwds):
  49. def decorate(f):
  50. for k in kwds:
  51. setattr(f, k, kwds[k])
  52. return f
  53.  
  54. return decorate
  55.  
  56.  
  57. @attrs(versionadded="2.2",
  58. author="Guido van Rossum")
  59. def mymethod(f):
  60. print(getattr(mymethod,'versionadded',0))
  61. print(getattr(mymethod,'author',0))
  62. print(f)
  63.  
  64. if __name__ == "__main__":
  65. # x = MyClass()
  66. # y = MyClass()
  67. mymethod(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement