NameError:全局名称“getAllElements”调用类函数时未定义

发布于 01-04 04:46 字数 2298 浏览 5 评论 0原文

我可能忽略了一些东西,但我得到了一个带有函数“getAllElements”的类“parse”。在主脚本中,我使用

from parseXML import parse. 

“然后”

parse = parse(file) 

导入解析,效果很好。但是当我这样做时,

print parseXML.parse(file).getAllElements()

我收到以下错误:

NameError: global name 'getAllElements' is not defined 

下面是代码。我哪里出错了?

编辑:更改注释后的代码

class parse:
    # Constructor
    def __init__(self, file):
        # parse the xml file into a tree
        tree = xml.parse('/homes/ndeklein/test.featureXML')
        # Get the root node of the xml file
        self.rootElement = tree.getroot()
        # Set self.parent to rootElement, because the first element won't have a parent (because it is the root)
        self.parent = 'rootElement'
        # dictionary that contains the parent -> child relation
        self.parentChildDict = {}

    # Go recursively through all the elements in the xml file, starting at the choosen rootElement, until only leaves (elements that don't contain elements) are left
    # Return all the elements from the xml file
    def getAllElements(self):
        # if this is the first time this parent is seen:
        #     make elementDict with parent as key and child as value in a list
        if not self.parentChildDict.has_key(self.parent):
            self.parentChildDict[self.parent] = [self.rootElement]
        # else: add the child to the parent dictionary
        else:
            self.parentChildDict[self.parent].append(self.rootElement)
        for node in self.rootElement:
            # if the len of rootElement > 0 (there are more elements in the element):
            #    set self.parent to be node and recursively call getAllElements
            if len(self.rootElement) > 0:
                self.parent = node
                getAllElements()
        return self.parentChildDict

#!/usr/bin/env python

# author: ndeklein
# date: 08/02/2012
# function: calls out the script

import parseXML
import xml.etree.cElementTree as xml
import sys

#Parse XML directly from the file path
file = '/homes/ndeklein/EP-B1.featureXML'
# parse the xml file into a tree
print parseXML.parse(file).getAllElements()

I'm probably overlooking something, but I got a class "parse" with a function "getAllElements". In the main script I import parse using

from parseXML import parse. 

Then I do

parse = parse(file) 

which works fine. But when I do

print parseXML.parse(file).getAllElements()

I get the following error:

NameError: global name 'getAllElements' is not defined 

Below is the code. Where am I going wrong?

Edit: changed the code after comment

class parse:
    # Constructor
    def __init__(self, file):
        # parse the xml file into a tree
        tree = xml.parse('/homes/ndeklein/test.featureXML')
        # Get the root node of the xml file
        self.rootElement = tree.getroot()
        # Set self.parent to rootElement, because the first element won't have a parent (because it is the root)
        self.parent = 'rootElement'
        # dictionary that contains the parent -> child relation
        self.parentChildDict = {}

    # Go recursively through all the elements in the xml file, starting at the choosen rootElement, until only leaves (elements that don't contain elements) are left
    # Return all the elements from the xml file
    def getAllElements(self):
        # if this is the first time this parent is seen:
        #     make elementDict with parent as key and child as value in a list
        if not self.parentChildDict.has_key(self.parent):
            self.parentChildDict[self.parent] = [self.rootElement]
        # else: add the child to the parent dictionary
        else:
            self.parentChildDict[self.parent].append(self.rootElement)
        for node in self.rootElement:
            # if the len of rootElement > 0 (there are more elements in the element):
            #    set self.parent to be node and recursively call getAllElements
            if len(self.rootElement) > 0:
                self.parent = node
                getAllElements()
        return self.parentChildDict

.

#!/usr/bin/env python

# author: ndeklein
# date: 08/02/2012
# function: calls out the script

import parseXML
import xml.etree.cElementTree as xml
import sys

#Parse XML directly from the file path
file = '/homes/ndeklein/EP-B1.featureXML'
# parse the xml file into a tree
print parseXML.parse(file).getAllElements()

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

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

发布评论

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

评论(2

半步萧音过轻尘2025-01-11 04:46:03

正如普拉文提到的,这是你的重要性和通话风格。

因为您以这种方式导入:

from foo import bar

您不需要(实际上不应该)在调用中显式声明 foo。

bar.baz()

不是

foo.bar.baz()

这样,在你的情况下尝试调用:

parse(file).getAllElements()

但是你仍然需要在递归中解决裸调用:
getAllElements() 可能应该是 self.getAllElements()

It's your import and style of call, as Praveen alluded to.

Because you import in this fashion:

from foo import bar

You don't need to (and in fact shouldn't) explicitly declare foo in your call.

bar.baz()

Not

foo.bar.baz()

So in your case try the call:

parse(file).getAllElements()

But you still need to address the naked call in your recursion:
getAllElements() probably should be self.getAllElements()

谁人与我共长歌2025-01-11 04:46:03

看起来 parseXML 不在您的本地命名空间中,因为您正在执行 from parseXML import parse。您是否尝试直接导入 parseXML 并执行此操作?

import parseXML 
parseXML.parse(file).getAllElements()

您收到 NameError 是因为您不能仅在方法内调用 getAllElements() 。它必须是 self.getAllElements()。

正如评论中提到的,代码中还存在其他逻辑错误,您需要更正。

Looks like parseXML is not in your local namespace, because you're doing from parseXML import parse. Did you try importing parseXML directly and doing this instead?

import parseXML 
parseXML.parse(file).getAllElements()

You're getting NameError because you can't just call getAllElements() inside the method. It needs to be self.getAllElements().

As mentioned in the comments, there are other logical errors in the code that you will need to correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文