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还没初始化。

      

      

  • 相关阅读:
    ymnets----框架内功能了解
    ymnets----代码生成
    关于分层架构
    SQL——抛出错误
    Jump Game
    combination sum && combination sum II
    35-Search Insert Position
    33-Search in Rotated Sorted Array
    34- Find First and Last Position of Element in Sorted Array
    机器学习实战之SVM
  • 原文地址:https://www.cnblogs.com/Fmaj7/p/13167656.html
Copyright © 2011-2022 走看看