What is the difference between class and instance attributes?

后端 未结 5 1589
面向向阳花
面向向阳花 2020-11-21 05:44

Is there any meaningful distinction between:

class A(object):
    foo = 5   # some default value

vs.

class B(object):
    d         


        
5条回答
  •  忘了有多久
    2020-11-21 06:24

    The difference is that the attribute on the class is shared by all instances. The attribute on an instance is unique to that instance.

    If coming from C++, attributes on the class are more like static member variables.

提交回复
热议问题