So I keep getting this error that when I Google for it, the most common fix for it is to be sure that all the methods of a class have ‘self’ as the first argument. Here is the error:
File "C:\Users\me\Documents\Project\code\model\TrainEvent.py", line 9, in
NameError: global name 'self' is not definedPress any key to continue . . .
This seems like a common error with a simple fix except all my methods do have self as the first argument. Then I realized the traceback is simply pointing at the line where the class name is declared. It is not pointing at a line where I call a method with self. This error is being raised long before that. In fact, it seems to be raised upon importing. Here is TrainEvent.py.
from xml.etree.ElementTree import Element
class TrainEvent(object):
def __init__(self, element):
self._element = element
self.MsgId = self.__tagGrab('MsgId')
self.MsgTime = self.__tagGrab('MsgTime')
self.Offset = self.__tagGrab('Offset')
self.TranId = self.__tagGrab('TranId')
self.Portal = self.__tagGrab('Portal')
moveElem = self._element.find('Move')
self.StartTime = self.__tagGrab('StartTime', moveElem)
self.EndTime = self.__tagGrab('EndTime', moveElem)
self.Type = self.__tagGrab('Type', moveElem)
carElem = self._element.find('Car')
self.Name = self.__tagGrab('Name', carElem)
self.UniqueId = self.__tagGrab('UniqueId', carElem)
self.Orientation = self.__tagGrab('Orientation', carElem)
self.Wells = self.__tagGrab('Wells', carElem)
self.Axles = self.__tagGrab('Axles', carElem)
self.Length = self.__tagGrab('Length', carElem)
self.IsEngine = self.__tagGrab('IsEngine', carElem)
self.IsGhost = self.__tagGrab('IsGhost', carElem)
def getTree(self):
aTree = Element('ApsMessage')
self.__addTag(aTree, 'MsgId', self.MsgId)
self.__addTag(aTree, 'MsgTime', self.MsgTime)
self.__addTag(aTree, 'Offset', self.Offset)
self.__addTag(aTree, 'TranId', self.TranId)
self.__addTag(aTree, 'Portal', self.Portal)
moveElem = Element('Move')
self.__addTag(moveElem, 'StartTime', self.StartTime)
self.__addTag(moveElem, 'EndTime', self.EndTime)
self.__addTag(moveElem, 'Type', self.Type)
aTree.append(moveElem)
carElem = Element('Car')
self.__addTag(carElem, 'Name', self.Name)
self.__addTag(carElem, 'UniqueId', self.UniqueId)
self.__addTag(carElem, 'Orientation', self.Orientation)
self.__addTag(carElem, 'Wells', self.Wells)
self.__addTag(carElem, 'Axles', self.Axles)
self.__addTag(carElem, 'Length', self.Length)
self.__addTag(carElem, 'IsEngine', self.IsEngine)
self.__addTag(carElem, 'IsGhost', self.IsGhost)
aTree.append(carElem)
return aTree
def getTag(self):
return self._element.tag
def __tagGrab(self, tagName, parent=self._element):
'''
Helper function for XML reading operations.
'''
return parent.find(tagName).text
def __addTag(self, element, tagName, textValue=None):
'''
Helper function for setting values for XML elements. Note that this
function assumes unique tag name values within element.
'''
element.append(Element(tagName))
if textValue:
element.find(tagName).text = str(textValue)
So if all my methods have self for a first argument and the call stack was pointing at the class declaration as the problem, but for self not being defined globally, then what did I do wrong here?
PS: If it helps at all, I am using the latest IronPython interpreter in case the problem is IronPython specific for some reason.
The problem is in your
__tagGrabfunction:You cannot have
selfin the header — rather haveNoneand then correct in the body:The reason is that when the class object is being created there is no
self; besidesglobals()(which hasElementplus a couple other items), the only names defined when Python gets to__tagGrabare__module__,__init__,getTree, andgetTag.As an experiment to prove this to yourself, try this: