difference between variables inside and outside of __init__()

前端 未结 10 800
醉酒成梦
醉酒成梦 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:45

    further to S.Lott's reply, class variables get passed to metaclass new method and can be accessed through the dictionary when a metaclass is defined. So, class variables can be accessed even before classes are created and instantiated.

    for example:

    class meta(type):
        def __new__(cls,name,bases,dicto):
              # two chars missing in original of next line ...
              if dicto['class_var'] == 'A':
                 print 'There'
    class proxyclass(object):
          class_var = 'A'
          __metaclass__ = meta
          ...
          ...
    

提交回复
热议问题