装饰器
闭包装饰器
装饰器的本质就是一个闭包, 闭包三要素:1 函数嵌套, 2 外部函数返回内部函数的引用, 内部函数使用外部函数的变量
1 使用装饰器简单实现一个登录校验功能
In [1]: user = {"username": "123", "password": 123}
def outter(func):
...: def inner(*args, **kwargs):
...: if user.get('token'):
...: return func(*args, **kwargs)
...: else:
...: username = input("user:")
...: password = input("pwd:")
...: if user.get("username") == username:
...: user['token'] = True
...: print("login success")
...: return func(*args, **kwargs)
...: else:
...: raise ValueError('login faild')
...: return inner
...:
In [8]: @outter # func = outer(tests)
...: def tests(a, b):
...: print('tests result:',a*b)
...:
In [9]: tests(2,4) # outer(tests)(2, 4)
user:123
pwd:123
login success
tests result: 8
In [10]: tests(2,4)
tests result: 8
In [11]: tests(2,9)
tests result: 18
类中使用的默认装饰器
@classmethod
类中装饰@classmethod魔法方后就属于类方法, 传的第一个参数就是类对象, 可以直接被类对象调用,类方法也可以被实例方法调用,但是类方法不能调用实例方法
使用场景:对于不需要实例化调用的时候调用
class Method:
...: def __init__(self):
...: pass
...: @classmethod # 装饰器修饰后变成类方法,可以类直接调用
...: def trf(cls):
...: print(cls)
...: def test(self): # 实例方法
...: print(self)
...: self.trf()
...:
...:
In [23]: Method.trf()
<class '__main__.Method'>
In [24]: Method.test()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-23908d4c4445> in <module>
----> 1 Method.test()
TypeError: test() missing 1 required positional argument: 'self'
In [25]: s = Method()
In [26]: s.trf()
<class '__main__.Method'>
In [27]: s.test()
<__main__.Method object at 0x000001ADD8BE3C18>
<class '__main__.Method'>
@staticmethod 方法
staticmethod 方法修饰的实例方法可以不传参数, 跟正常的魔法方法一样
@property 方法
设置property装饰器可以通过实例调用属性一样调用实例,
使用场景:适用于不想被修改的属性可以使用这个装饰
类的魔法方法
new 方法的使用
在类中, 创建实例对象调的第一个方法就是__new__ 魔法方法,new方法给对象分配内存, 所以每个实例初始化时走的一个方法就是new方法
使用场景:常通过这个魔法方法设置单例
In [38]: class bdss:
...: sunclss = None # 非单例模式下,只要类属性被改变,下一个实例就使用的时被改变的类属性值
...: def __new__(cls, *args, **kwargs):
...: print(cls.sunclss)
...: from random import randint
...: cls.sunclss = randint(1, 40)
...: print('-------',cls.sunclss)
...: return super().__new__(cls, *args, **kwargs)
In [39]: a = bdss()
None
------- 2
In [40]: b = bdss()
2
------- 33
In [41]: c = bdss()
33
------- 37
# 单例实现
In [28]: class bdss:
...: sunclss = None
...: def __new__(cls, *args, **kwargs):
...: if cls.sunclss:
...: return cls.sunclss
...: else:
...: cls.sunclss = super().__new__(cls, *args, **kwargs)
...: return cls.sunclss
...:
...:
In [29]: a = bdss()
In [30]: a
Out[30]: <__main__.bdss at 0x1add90c69b0>
In [31]: b = bdss()
In [32]: b
Out[32]: <__main__.bdss at 0x1add90c69b0>
__call__魔法方法的使用
类中定义了__call__魔法方法可以史类对象像调用函数一样调用,得到结果
In [50]: class A:
...: def __init__(self):
...: pass
...: def __call__(self):
...: print('call method')
...:
In [51]:
In [51]: a = A()
In [52]: a() # 实例属性像调用函数一样调用
call method
使用类装饰器完成单例模式
1 调用装饰器的本质是 外部函数引用内部函数方法,
,所以要用类实现装饰器就也就要实现调用类方法时触发某个类方法, 而__call__方法是调用函数时进行调用的,所以使用类方法做装饰器就必须要call方法
类中常用的三大魔法方法(getattr, setattr, delattr)
__getattr__和 getattribute 方法
1 getattribute 在类获取属性时进行调用, 如果类方法中没找到该属性就会触发该getattr方法,父类的getattr方法没有会抛异常,所以如果想不抛异常可以重写getattr方法,使他不抛异常
class A:
...: def __init__(self):
...: self.a = 'a'
...: self.b = 'b'
...:
...: def __getattr__(self, key):
...: print('没获取到该值时有异常调用该')
...: object.__getattribute__(self, key)
...:
...:
In [10]: x = A()
In [11]: x.c
没获取到该值时有异常调用该
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-a693afdd9058> in <module>
----> 1 x.c
<ipython-input-9-878ca7145054> in __getattr__(self, key)
6 def __getattr__(self, key):
7 print('没获取到该值时有异常调用该')
----> 8 object.__getattribute__(self, key)
9
10
AttributeError: 'A' object has no attribute 'c'
2 setattr魔法方法在设置属性的时候调用, 如果有逻辑要校验可以重写这种方法,使用的场景较多的是如果要将python中的某个键变成实例属性可以用setattr方法
n [9]: class A:
...: def __init__(self):
...: self.a = 'a'
...: self.b = 'b'
...:
...: def __setattr__(self, item, value):
...: print('__setattr__')
...: object.__setattr__(self, item, value)
...:
...:
...:
...: def __delattr__(self, item):
...: print('属性删了')
...: object.__delattr__(self, item)
...:
...: def __getattr__(self, key):
...: print('没获取到该值时有异常调用该')
...: object.__geta
...: ttribute__(self, key)
...:
In [10]: xi = A()
__setattr__
__setattr__
In [11]: del xi.a
属性删了