Python class members different from instance members

Ronald :

I'm trying to get the value of WxPython checkboxes. When I run the following command inside my Class:

print(self)
a = dir(self)
print(a)

#result
<__main__.Window object at 0x03B02670>
['AcceleratorTable', 'AcceptsFocus', etc...
 'm_staticText3', 'm_staticText31', 'm_staticText311', 'm_staticText3111', 'm_staticText3112', 'm_staticText31121', 'm_staticline1', 'm_staticline3']

My checkboxes are part of the returned result. But when I substitute the 'self' for the class 'Window', the checkbox attributes are missing!

print(Window)
a = dir(Window)
print(a)

#result
<class '__main__.Window'>
['AcceleratorTable', 'AcceptsFocus', etc..,
 'WindowVariant', '__bool__', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

It looks the same, but my checkboxes are not returned! What's going on here?

chuck2002 :

A class such as Window is not instantiated. Therefore it can't access anything that requires a class instance. In the following code:

class A:
    b = 0

    def __init__(self):
        self.a = 1

print(dir(A))
inst = A()
print(dir(inst))

dir(A) will not contain a, because access to a requires instantiation as it is declared individually for each instance in the __init__ method. It will contain b, which is static (belongs to the class itself and not its instances). dir(inst) will contain both a and b.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20457&siteId=1