Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:55:29+00:00 2026-05-26T19:55:29+00:00

So I keep getting this error that when I Google for it, the most

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:55:29+00:00Added an answer on May 26, 2026 at 7:55 pm

    The problem is in your __tagGrab function:

    def __tagGrab(self, tagName, parent=self._element):
    

    You cannot have self in the header — rather have None and then correct in the body:

    def __tagGrab(self, tagName, parent=None):
        if parent is None:
            parent = self._element
        ...
    

    The reason is that when the class object is being created there is no self; besides globals() (which has Element plus a couple other items), the only names defined when Python gets to __tagGrab are __module__, __init__, getTree, and getTag.

    As an experiment to prove this to yourself, try this:

    class TestClassCreation(object):
        print("Started creating class")
        print("names so far: %s" % vars())
    
        def __init__(self):
            pass
        print("now we have %s" % vars())
    
        def noop(self, default=None):
            print("this gets run when noop is called")
        print("and now have %s" % vars())
        print()
    
        print("and now we'll fail...")
        def failure(self, some_arg=self.noop):
            pass
        print("we never get here...")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a singleton class that loads a plist. I keep getting this error
I keep getting this error all over the place where I only have jquery
I keep getting this error: MySQL said: #1064 - You have an error in
I keep getting this error: Invalid Signature - This error occurs when you have
When I run the following code, I keep getting this error: Traceback (most recent
Keep getting this error after inserting a subdatasheet into a query and trying to
I keep getting this error System.Web.HttpException was unhandled by user code Message=Validation of viewstate
I keep getting this error when I try to commit a group of executed
I keep getting this error when I try to call Find() public void findTxt(string
I keep getting this error whenever I call gethostbyname() in my C code. ==7983==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.