Python - 从类主体内部引用类名

发布于 2024-09-10 17:40:23 字数 342 浏览 7 评论 0原文

在Python中,我想要一个类属性,一个带有初始化值的字典。我编写了这段代码:

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, 
                         '#':MetaDataElement.MD_TAG}

但是当我尝试运行此代码时,我收到一条错误消息“NameError:名称'MetaDataElement'未定义”。你能帮我吗?

提前致谢。

In Python, I want to have a class attribute, a dictionary, with initialized values. I wrote this code:

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, 
                         '#':MetaDataElement.MD_TAG}

But when I try to run this code, I get an error message with "NameError: name 'MetaDataElement' is not defined". Could you help me?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热鲨 2024-09-17 17:40:23

您无法在构建时引用 MetaDataElement,因为它还不存在。因此,

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, 
                         '#':MetaDataElement.MD_TAG}

失败是因为 mapInitiator2Type 的构造本身要求 MetaDataElement 具有属性,而它还没有。您可以将常量 MD_INVALID 等视为类构造的本地变量。这就是为什么以下内容有效,正如 icktoofay 所写的:

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!': MD_CATEGORY,  # MD_CATEGORY is like a local variable!
                         '#': MD_TAG}

但是,您可以在任何尚未解释的代码段中引用类 MetaDataElement ,就像

    def method_of_MetaDataElement(self):
        print MetaDataElement.MD_TAG

您甚至have to这里参考MetaDataElement,因为执行method_of_MetaDataElement()时,MD_TAG不是一种局部变量(MD_TAG 仅在类构造期间被定义为一种局部变量)。创建 MetaDataElement 类后,MD_TAG 只是一个类属性,这就是 method_of_MetaDataElement() 必须这样引用它的原因。

You cannot refer to MetaDataElement while it is being constructed, since it does not yet exist. Thus,

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!':MetaDataElement.MD_CATEGORY, 
                         '#':MetaDataElement.MD_TAG}

fails because the very construction of mapInitiator2Type requires MetaDataElement to have attributes, which it does not yet have. You can think of your constants MD_INVALID, etc. as variables that are local to the construction of your class. This is why the following works, as icktoofay wrote:

class MetaDataElement:
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!': MD_CATEGORY,  # MD_CATEGORY is like a local variable!
                         '#': MD_TAG}

However, you can refer to the class MetaDataElement in any yet un-interpreted piece of code, as in

    def method_of_MetaDataElement(self):
        print MetaDataElement.MD_TAG

You even have to refer to MetaDataElement, here, because MD_TAG is not a kind of local variable when method_of_MetaDataElement() is executed (MD_TAG was only defined as a kind of local variable during class construction). Once the class MetaDataElement is created, MD_TAG is simply a class attribute, which is why method_of_MetaDataElement() must refer to it as such.

絕版丫頭 2024-09-17 17:40:23

首先,您使用的是旧式类。您可能应该使用新式类,如下所示:

class MetaDataElement(object):
    ...

注意 (object)。但无论如何,只需在引用类属性时删除 MetaDataElement. 即可。完成后会是这样的:

class MetaDataElement(object):
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!': MD_CATEGORY, 
                         '#': MD_TAG}

First of all, you're using old-style classes. You should probably use new-style classes, like so:

class MetaDataElement(object):
    ...

Note the (object). Anyway, though, simply remove the MetaDataElement. when referring to the class attributes. This is what it'd look like when that's done:

class MetaDataElement(object):
    (MD_INVALID, MD_CATEGORY, MD_TAG) = range(3)
    mapInitiator2Type = {'!': MD_CATEGORY, 
                         '#': MD_TAG}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文