Python 类成员与实例成员不同

标签 python checkbox directory attributes wxpython

我正在尝试获取 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)将包含 ab .

关于Python 类成员与实例成员不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60488525/

相关文章:

php - 当用户选中复选框时更改 php 数组

perl - 如何按排序顺序读取目录中的文件?

python - 即使专门包含,cx-freeze 也无法包含模块

java - 无法使用相对定位器单击第二个复选框

python - pandas:DTypeWarning,但我指定了 dtypes

python - 如何在 Python 的 Tkinter 中获取多个 Checkbutton 的文本值列表?

iis - 网页无法访问文件夹。 (IIS)

macos - 在 Mac 上将文件夹显示为文件

python - virtualenv 在 Ubuntu 上创建名为 'local' 的目录

python - 在不同设备上进行程序间通信的最简洁方法