推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
PanameraM
V2EX  ›  Python

Python setattr()也__setattr__有什么区别联系呢?看了个 stackoverflow 答案,不是太懂,各位求解释下。

  •  
  •   PanameraM · Mar 2, 2017 · 3854 views
    This topic created in 3543 days ago, the information mentioned may be changed or developed.
    1. setattr(obj, attr, value) just calls obj.setattr(attr, value), at least for new-style classes.

    2. setattr(instance, name, value) is syntactic sugar for instance.setattr(name, value).

    3. You would only need to call object.setattr(...) inside a class definition, and then only if directly subclassing object -- if you were subclassing something else, Spam for example, then you should either use super() to get the next item in the heirarchy, or call Spam.setattr(...) -- this way you don't risk missing behavior that super-classes have defined by skipping over them directly to object.

    4. The code is probably using object.setattr for the exact purpose of skipping the superclass's setattr

    5. 英文好的麻烦给翻译翻译上面的 4 段话的意思。感谢!

    10 replies  •  2017-03-02 13:36:29 +08:00
    czheo
    czheo
    Mar 2, 2017
    1
    setattr(instance, name, value)是 instance.__setattr__(name, value)的语法糖。
    rwecho
    rwecho
    Mar 2, 2017
    2
    setattr(instance, name, value) 是静态方法,意思为 给一个实例赋值一个属性值

    instance.setattr(name, value) 是面向对象方法, 意思为一个实例设置一个属性值.
    dant
    dant
    Mar 2, 2017 via Android
    3
    setattr 是你在外部调用的
    __setattr__ 是你在类里面实现的
    PanameraM
    PanameraM
    OP
    Mar 2, 2017
    4
    @dant 意思就是我在外部用 setattr 其实是调用类里面的__setattr__方法喽?
    PanameraM
    PanameraM
    OP
    Mar 2, 2017
    5
    其实我主要想知道第三段和第四段的意思?
    PanameraM
    PanameraM
    OP
    Mar 2, 2017
    6
    @rwecho 那么这两者的功能是等价的吗?
    rwecho
    rwecho
    Mar 2, 2017❤️1
    7
    等价的.

    我理解的意思是:

    在类里面可以调用 self.attr(*) 也可以调用 super().attr(*). 这是继承的概念
    调用 instance.attr(*)只会执行 instance 的 attr 的方法,从而隐藏了 super(*).

    这在 OO 思想里面就是把静态方法变成实例方法, 是思想里面的差距, 和使用功能上没有差距.
    另外通过 instance.attr(*) 可以达到链式表达式的效果
    dant
    dant
    Mar 2, 2017
    8
    @Tianny 你可以实现 __setattr__ 来改变属性被赋值时的行为
    fy
    fy
    Mar 2, 2017
    9
    5. 英文也不好,谢谢。
    sunsol
    sunsol
    Mar 2, 2017❤️1
    10
    setattr(obj,name,value)相当于 obj.name=value
    而作为函数可以更方便调用,通常用于 name 为变动值时。

    __setattr__(self,name,value)是自定义对象方法,
    用来重新定义对像的 obj.name=value 时操作
    默认行为为 self.__dict__[name]=value,
    通常用于设置检查 value 是否合法的情况。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4881 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 02:45 · PVG 10:45 · LAX 18:45 · JFK 21:45
    ♥ Do have faith in what you're doing.