python class attribute

后端 未结 3 1349
悲&欢浪女
悲&欢浪女 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:36

    Another way (perhaps a more concise one) to demonstrate this:

    class A():
       a1 = []
    x = A()
    y = A()
    x.a1.append("test")
    x.a1, y.a1
    (['test'], ['test'])
    
    class B():
       b1 = None
       def __init__(self):
          self.b1 = list()
    r = B()
    s = B()
    r.b1.append("test")
    r.b1, s.b1
    (["test"], [])
    

提交回复
热议问题