Error msg "NameError: global name 'atomName' is not defined" when calling class fnct.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jorgejch
    New Member
    • Nov 2009
    • 2

    Error msg "NameError: global name 'atomName' is not defined" when calling class fnct.

    Hello, I've started with python (3) recently. Initialy only for scripting. Now I'm trying the object oriented bit.

    I'm getting the following error message
    Code:
    <Atom.Atom object at 0x7f0b09597fd0>
    Traceback (most recent call last):
      File "./Main.py", line 7, in <module>
        print (t.getAtomName())
      File "/home/jorge/Documentos/projetos/mestrado/códigos/cartesian_zmatrix/Atom.py", line 22, in getAtomName
        return atomName
    NameError: global name 'atomName' is not defined
    when calling the function 'getAtomName()' of the 'Atom' class. This class is on the file Atom.py on the same directory as the test code.

    below is my test code and Atom class code:

    Code:
     #! /usr/bin/python3
    
    from Atom import *
    
    t = Atom("C",12)
    print (t.getAtomName())
    Code:
    class Atom:
    
        cartesian = dict()
        bondList = list()
        atomName = str()
        atomicNum = int()
    
        def __init__(self,name, atNum):
            self.atomName = name
            self.atomicNum = atNum
    
        def setCoordinates(x, y, z):
            catesian['x'] = x
            catesian['y'] = y
            catesian['z'] = z
    
        def addBondedAtom(Atom):
            bondList.append(Atom)
    
        def getAtomName():
            return atomName
        def getAtomicNumber():
            return atomicNum
        def getBondList():
            return bondList
        def getCartesian():
            return cartesian
    I appreciate any help, and also any tip you might have considering I've just started with python.

    Abraço,
    Jorge
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Ah.

    You'll need to change your getAtomName function to the following:
    Code:
         def getAtomName(self):
             return self.atomName
    atomName is only defined for an object within your class. It's not a "global" variable, ie one that's available to all objects.
    Similar things go for other parts of your class. Maybe if you tell us a bit about how you're going to use it. I think, at the least, that atomName and atomicNum are object specific and don't need to be declared up front.

    I'm also no expert, but in general you'll probably want
    Code:
    class Atom(object):
    for various reasons which I can't remember off hand. I think this might make the global variables work better...

    Good luck

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Code:
      class Atom(object):
      Classes created without inheriting from object are called classic classes or old-style classes. They are still supported for backward compatibility. Prior to Python version 2.2, classes and objects were implemented using an entirely different mechanism which is deprecated.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Yeah, I know there's some technical stuff, but as a user of python, I just know that unless you have a good reason for it inheriting object is generally preferrable. It's not like it's difficult to do...

        Comment

        • jorgejch
          New Member
          • Nov 2009
          • 2

          #5
          Hello Guys, thank you for the replies. The problem was the 'self.' bit.

          Comment

          Working...