在werkzeug中有这样的定义:
class Local(object):
__slots__ = ('__storage__', '__ident_func__')
def __init__(self):
object.__setattr__(self, '__storage__', {})
object.__setattr__(self, '__ident_func__', get_ident)
此处的__init__方法里,使用self.__storage__ = {}的话是否效果一样呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
效果其实是一样的。
比如
object.a = 1最终也是调用self.__setattr__('a', 1);只不过你自己重写父类(object)的__setattr__方法时,可以加上自己定义的一些规则,比如: