python: class attributes and instance attributes

后端 未结 6 980
滥情空心
滥情空心 2020-12-01 19:54

I\'m new to python and learned that class attributes are like static data members in C++. However, I got confused after trying the following code:

>>&g         


        
6条回答
  •  抹茶落季
    2020-12-01 20:12

    When you assign an attribute in python, it doesn't matter where that attribute might already be defined, the new assignment is always applied to the object assigned to. When you say

    >>> f1.a=5
    

    The object that has the attribute here is the instance, so it's the instance that gets the new value.

提交回复
热议问题