zoukankan      html  css  js  c++  java
  • Python super方法及__setattr__方法详解

        def __setattr__(self, name, value):
            """
            Set the value of setting. Clear all cached values if _wrapped changes
            (@override_settings does this) or clear single values when set.
            """
            if name == '_wrapped':
                self.__dict__.clear()
            else:
                self.__dict__.pop(name, None)
            super(LazySettings, self).__setattr__(name, value)
    
        1、执行完当前类的_setattr__方法再执行LazySettings类的父类的__setattr__方法
        2、当类含有__setattr__方法时,对象初始化自动执行该方法。

        3、在实例化的时候,会进行初始化,在__init__里,对value的属性值进行了设置,这时候会调用__setattr__方法。

        在对name重新设置值的时候,会再次进入__setattr__方法。

        需要注意的地方是,在重写__setattr__方法的时候千万不要重复调用造成死循环。

      4、self.__dict__['_wrapped']用于获取类属性,__setattr__方法中不可通过对象self._wrapped获取类属性,因为此方法执行完之前_wrapped还没初始化。

      

      

  • 相关阅读:
    html5全局属性
    net包之Lookup
    net包之dial拨号和listen监听
    net包之IPConn
    利用 RequireJS 进行依赖项管理
    canvas 学习资料
    net包之UDPConn
    LABjs、RequireJS、SeaJS 哪个最好用?为什么?
    WIA
    Mac OS 10.x.x安装在Vmware虚拟机上!
  • 原文地址:https://www.cnblogs.com/Fmaj7/p/13167656.html
Copyright © 2011-2022 走看看