尝试在类自身的类变量部分中使用类的名称时发生问题 [英] Issue trying to use a class's name in it's own Class Variable section

查看:215
本文介绍了尝试在类自身的类变量部分中使用类的名称时发生问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在Python和PyQt写一个应用程序,我有一个问题。这个问题不需要知道PyQt本身,只是python中的静态变量。我在向一个类添加一些信号,当信号跳闸时,它会发出类的实例。

I'm writing an app in Python and PyQt right now, and I've having a bit of an issue. This problem doesn't require an knowledge of PyQt itself, just that of static variables in python. I'm tyring to add some signals to a class, which will emit the instance of the class when the signal is tripped.

我有一个类似这样的: / p>

What I have is something like this:

class Foo(QObject):
    # ...
    # Signals
    updated = pyqtSignal(Foo)
    moved = pyqtSignal(Foo)
    # ...

Python给我的错误:

Python is giving me the error:

NameError: name 'Foo' is not defined

IIRC,当类 Foo 绑定到全局变量时,我也不能使用 self

IIRC, this has to do something with when the class Foo is bound to the globals. I also can't use self here either. Any help on how to fix this issue would be appreciated.

推荐答案

问题是语句更新= c> ,则 = pyqtSignal(Foo) code>不存在当语句评估时。在一般情况下,你必须移动它的类定义,虽然可能有一些pyqt魔术(例如使用 QObject ,而不是另一个答案中所述的 Foo ):

The problem is that the statement updated = pyqtSignal(Foo) is evaluated while Foo is being constructed, so Foo doesn't exist when that statement is evaluated.. In the general case, you'd have to move it outside the class definition, although there may be some pyqt magic (e.g. using QObject rather than Foo as described in the other answer):

class Foo(...):
   ...

Foo.updated = pyqtSignal(Foo)

这篇关于尝试在类自身的类变量部分中使用类的名称时发生问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆