Compound assignment to Python class and instance variables

前端 未结 5 1960
天涯浪人
天涯浪人 2020-12-10 18:04

I\'ve been trying to understand Python\'s handling of class and instance variables. In particular, I found this answer quite helpful. Basically it says that if you declare a

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 18:46

    I think you just found a bug in Python there. bar.num += 1 should be an error, instead, it is creating an attribute num in the object bar, distinct from Foo.num.

    It's a really strange behavior.

提交回复
热议问题