python class attribute

后端 未结 3 1351
悲&欢浪女
悲&欢浪女 2020-11-27 20:05

i have a question about class attribute in python.

class base :
    def __init__ (self):
        pass
    derived_val = 1

t1 = base()
t2 = base ()

t2.deriv         


        
3条回答
  •  抹茶落季
    2020-11-27 20:41

    Check it out here and here.

    The __class__ attribute is the class that the object belongs to. So in your example, the situation is similar to static variables. The t2.__class__.derived_val is referring to the variable that belongs to the class, not the variable that belongs to t2.

提交回复
热议问题