difference between variables inside and outside of __init__()

前端 未结 10 798
醉酒成梦
醉酒成梦 2020-11-22 17:08

Is there any difference at all between these classes besides the name?

class WithClass ():
    def __init__(self):
        self.value = \"Bob\"
    def my_fu         


        
10条回答
  •  青春惊慌失措
    2020-11-22 17:41

    Variable set outside __init__ belong to the class. They're shared by all instances.

    Variables created inside __init__ (and all other method functions) and prefaced with self. belong to the object instance.

提交回复
热议问题