我正在尝试获取 WxPython 复选框的值。当我在我的类中运行以下命令时:
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”替换“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__']
看起来一样,但我的复选框没有返回!这是怎么回事?
最佳答案
诸如 Window
的类没有被实例化。因此它无法访问任何需要类实例的内容。在下面的代码中:
class A:
b = 0
def __init__(self):
self.a = 1
print(dir(A))
inst = A()
print(dir(inst))
dir(A)
不会包含a
,因为访问a
需要实例化,因为它是为 __init__
中的每个实例单独声明的方法。它将包含 b
,它是静态的(属于类本身而不是它的实例)。 dir(inst)
将包含 a
和b
.
关于Python 类成员与实例成员不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60488525/