Python class 个成员不同于实例成员
Python class members different from instance members
我正在尝试获取 WxPython 复选框的值。当我 运行 在我的 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']
我的复选框是返回结果的一部分。但是当我用 'self' 替换 class 'Window' 时,复选框属性丢失了!
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__']
看起来一样,但我的复选框没有返回!这是怎么回事?
一个class如Window
没有实例化。因此它无法访问任何需要 class 实例的东西。在以下代码中:
class A:
b = 0
def __init__(self):
self.a = 1
print(dir(A))
inst = A()
print(dir(inst))
dir(A)
不会包含 a
,因为访问 a
需要实例化,因为它是在 __init__
方法中为每个实例单独声明的。它将包含 b
,它是静态的(属于 class 本身而不是它的实例)。 dir(inst)
将包含 a
和 b
。
我正在尝试获取 WxPython 复选框的值。当我 运行 在我的 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']
我的复选框是返回结果的一部分。但是当我用 'self' 替换 class 'Window' 时,复选框属性丢失了!
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__']
看起来一样,但我的复选框没有返回!这是怎么回事?
一个class如Window
没有实例化。因此它无法访问任何需要 class 实例的东西。在以下代码中:
class A:
b = 0
def __init__(self):
self.a = 1
print(dir(A))
inst = A()
print(dir(inst))
dir(A)
不会包含 a
,因为访问 a
需要实例化,因为它是在 __init__
方法中为每个实例单独声明的。它将包含 b
,它是静态的(属于 class 本身而不是它的实例)。 dir(inst)
将包含 a
和 b
。