python中定义变量的问题

发布于 2024-10-08 10:03:56 字数 832 浏览 5 评论 0原文

我试图通过这段代码编写一些xml,

docs = XmlReportGenerator()
docs.AddMatchRow('FC Barcelona','Madryt','5:0')
docs.Save()

并且编写了自己的方法:

from lxml import etree

class XmlReportGenerator:
    """"""
    root = etree.Element('results')
    doc = etree.ElementTree(root)

    #----------------------------------------------------------------------
    def __init__(self):

        """""" 

    def AddMatchRow(self,teamA,teamB, score):
        pageElement = etree.SubElement(root,'Flight',teamA, teamB, score)

        """"""

    def Save(self,path = None):
        outFile = open('Matches.xml', 'w')
        doc.write(outFile) 

NameError: global name 'root' is not Defined 进程终止,退出代码为 1 完成

名称错误:未定义全局名称“doc” 进程终止,退出代码为 1 完成

我错过了什么吗?我是 python 的新手(我在 c# 方面有更多经验)。

I'm trying to write some xml by this piece of code

docs = XmlReportGenerator()
docs.AddMatchRow('FC Barcelona','Madryt','5:0')
docs.Save()

and I wrote my own method:

from lxml import etree

class XmlReportGenerator:
    """"""
    root = etree.Element('results')
    doc = etree.ElementTree(root)

    #----------------------------------------------------------------------
    def __init__(self):

        """""" 

    def AddMatchRow(self,teamA,teamB, score):
        pageElement = etree.SubElement(root,'Flight',teamA, teamB, score)

        """"""

    def Save(self,path = None):
        outFile = open('Matches.xml', 'w')
        doc.write(outFile) 

NameError: global name 'root' is not defined
Process terminated with an exit code of 1
done

NameError: global name 'doc' is not defined
Process terminated with an exit code of 1
done

Am I missing something? I'm a newbie in python (I have more experience in c#).

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

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

发布评论

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

评论(2

卷耳 2024-10-15 10:03:56

Python 是明确的。实例变量必须以 self. 开头。类变量必须前面加上类的名称。

这是一个固定版本。原始的 SubElement 调用也不正确:

from lxml import etree

# derive from 'object' if Python 2.X (it is default in Python 3.X)
class XmlReportGenerator(object):

    def __init__(self):
        # clearer to init instance variables here.
        self.root = etree.Element('results')
        self.doc = etree.ElementTree(self.root)

    def AddMatchRow(self,teamA,teamB, score):
        # Need self.root here
        pageElement = etree.SubElement(self.root,'Flight')
        # Added data elements (or did you want attributes?)
        etree.SubElement(pageElement,'teamA').text = teamA
        etree.SubElement(pageElement,'teamB').text = teamB
        etree.SubElement(pageElement,'score').text = score

    def Save(self,path = None):
        outFile = open('Matches.xml', 'w')
        # Need self.doc here
        self.doc.write(outFile)

# This code will run if the script is executed directly,
# but will be skipped if the script is imported by another script.
if __name__ == '__main__':
    docs = XmlReportGenerator()
    docs.AddMatchRow('FC Barcelona','Madryt','5:0')
    docs.Save()

Python is explicit. Instance variables must be prepended with self.. Class variables must be prepended with then name of the class.

Here's a fixed version. The original SubElement call was incorrect as well:

from lxml import etree

# derive from 'object' if Python 2.X (it is default in Python 3.X)
class XmlReportGenerator(object):

    def __init__(self):
        # clearer to init instance variables here.
        self.root = etree.Element('results')
        self.doc = etree.ElementTree(self.root)

    def AddMatchRow(self,teamA,teamB, score):
        # Need self.root here
        pageElement = etree.SubElement(self.root,'Flight')
        # Added data elements (or did you want attributes?)
        etree.SubElement(pageElement,'teamA').text = teamA
        etree.SubElement(pageElement,'teamB').text = teamB
        etree.SubElement(pageElement,'score').text = score

    def Save(self,path = None):
        outFile = open('Matches.xml', 'w')
        # Need self.doc here
        self.doc.write(outFile)

# This code will run if the script is executed directly,
# but will be skipped if the script is imported by another script.
if __name__ == '__main__':
    docs = XmlReportGenerator()
    docs.AddMatchRow('FC Barcelona','Madryt','5:0')
    docs.Save()
勿忘初心 2024-10-15 10:03:56

self 的存在是有原因的。使用self.root,而不是root

self is there for a reason. Use self.root, not root

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