Python 中的全局变量:执行比较时获取 NameError
Global variable in Python : Getting NameError while performing a comparison
我正在尝试在 python 中实现 SAX 解析器。读取 XML 并创建对象。下面是我的代码。
import xml.sax
class Employee :
def __init__(self, id):
self.id = id
class EmployeeHandler(xml.sax.ContentHandler):
emp = None
emplist = []
fName = False
lName = False
age = False
company = False
def __init__(self):
xml.sax.ContentHandler.__init__(self)
def startElement(self, name, attrs):
print("startElement '" + name + "'")
if name == "Employees" :
global emplist
emplist = []
if name == "Employee":
global emp
emp = Employee(attrs.getValue("id"))
elif name == "FirstName":
global fName
fName = True
elif name == "LastName":
global lName
lName = True
elif name == "Age":
global age
age = True
elif name == "Company":
global company
company = True
def characters(self, content):
print("characters '" + content + "'")
global fName, lName, age, company
if fName is True:
emp.firstName = content
elif lName is True:
emp.lastName = content
elif age is True:
emp.age = content
elif company is True:
emp.company = content
def endElement(self, name):
print("endElement '" + name + "'")
if name == "Employee":
#global emplist : To use list methods global is not required
emplist.append(emp)
def main(sourceFileName):
source = open(sourceFileName)
xml.sax.parse(source, EmployeeHandler())
if __name__ == "__main__":
main("EmployeeData")
print("Ids ", emplist[0].id , emplist[1].id)
执行代码时出现以下错误:
File "C:/Users/611834094/PycharmProjects/PractiseProject/xml_pratise/saxparser.py", line 47, in characters
if fName is True:
NameError: name 'fName' is not defined
确切地说,下面的块引发了错误:
def characters(self, content):
print("characters '" + content + "'")
global fName, lName, age, company
if fName is True:
emp.firstName = content
变量 fName
(连同其他全局变量)定义了一个初始值,该初始值在处理程序的 startElement()
方法中被更改,在字符方法中,我正在尝试将 fName 的值与 True
(布尔值)进行比较,我收到上述错误。
谢谢
这不起作用的原因是您没有定义任何全局变量。相反,您定义了 class 成员,它们不是全局范围的,而是对象范围内的。这些可以通过 self 参数访问,即 class 实例:
if self.fName:
self.emp.firstName = content
等等
我将只提取您的代码片段:
class EmployeeHandler(xml.sax.ContentHandler):
....
fName = False
....
elif name == "FirstName":
global fName
fName = True
....
global fName, lName, age, company
if fName is True:
global
关键字使 fName
成为全局 class 之外。要使用 class 变量 fName
,您应该在其前面加上 class 名称:
elif name == "FirstName":
EmployeeHandler.fName = True
....
if EmployeeHandler.fName is True:
话虽如此,none 的那些变量似乎适合作为 class 变量,看起来它们实际上应该是对象实例变量,即如@JesseBakker 所述附加到 self
。它们似乎是每个员工的属性,而不是整个员工的属性 class。
我正在尝试在 python 中实现 SAX 解析器。读取 XML 并创建对象。下面是我的代码。
import xml.sax
class Employee :
def __init__(self, id):
self.id = id
class EmployeeHandler(xml.sax.ContentHandler):
emp = None
emplist = []
fName = False
lName = False
age = False
company = False
def __init__(self):
xml.sax.ContentHandler.__init__(self)
def startElement(self, name, attrs):
print("startElement '" + name + "'")
if name == "Employees" :
global emplist
emplist = []
if name == "Employee":
global emp
emp = Employee(attrs.getValue("id"))
elif name == "FirstName":
global fName
fName = True
elif name == "LastName":
global lName
lName = True
elif name == "Age":
global age
age = True
elif name == "Company":
global company
company = True
def characters(self, content):
print("characters '" + content + "'")
global fName, lName, age, company
if fName is True:
emp.firstName = content
elif lName is True:
emp.lastName = content
elif age is True:
emp.age = content
elif company is True:
emp.company = content
def endElement(self, name):
print("endElement '" + name + "'")
if name == "Employee":
#global emplist : To use list methods global is not required
emplist.append(emp)
def main(sourceFileName):
source = open(sourceFileName)
xml.sax.parse(source, EmployeeHandler())
if __name__ == "__main__":
main("EmployeeData")
print("Ids ", emplist[0].id , emplist[1].id)
执行代码时出现以下错误:
File "C:/Users/611834094/PycharmProjects/PractiseProject/xml_pratise/saxparser.py", line 47, in characters
if fName is True:
NameError: name 'fName' is not defined
确切地说,下面的块引发了错误:
def characters(self, content):
print("characters '" + content + "'")
global fName, lName, age, company
if fName is True:
emp.firstName = content
变量 fName
(连同其他全局变量)定义了一个初始值,该初始值在处理程序的 startElement()
方法中被更改,在字符方法中,我正在尝试将 fName 的值与 True
(布尔值)进行比较,我收到上述错误。
谢谢
这不起作用的原因是您没有定义任何全局变量。相反,您定义了 class 成员,它们不是全局范围的,而是对象范围内的。这些可以通过 self 参数访问,即 class 实例:
if self.fName:
self.emp.firstName = content
等等
我将只提取您的代码片段:
class EmployeeHandler(xml.sax.ContentHandler):
....
fName = False
....
elif name == "FirstName":
global fName
fName = True
....
global fName, lName, age, company
if fName is True:
global
关键字使 fName
成为全局 class 之外。要使用 class 变量 fName
,您应该在其前面加上 class 名称:
elif name == "FirstName":
EmployeeHandler.fName = True
....
if EmployeeHandler.fName is True:
话虽如此,none 的那些变量似乎适合作为 class 变量,看起来它们实际上应该是对象实例变量,即如@JesseBakker 所述附加到 self
。它们似乎是每个员工的属性,而不是整个员工的属性 class。