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

<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:

#! /usr/bin/python3

from Atom import *

t = Atom("C",12)
print (t.getAtomName())
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

You were missing the self.XXXX, self. is used in python classes.

class Atom:

    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(self):
        return self.atomName
    def getAtomicNumber(self):
        return self.atomicNum
    def getBondList(self):
        return bondList
    def getCartesian(self):
        return cartesian

if __name__ == "__main__":
    t = Atom("C",12)
    print (t.getAtomName())
    print (t.getAtomicNumber())
Comments
Clear post.

You have both class instances and class objects. The variables
cartesian = dict()
bondList = list()
atomName = str()
atomicNum = int()
would point to the same variable (block of memory) for all instances of the class, where
self.atomName = name
self.atomicNum = atNum
are unique for each instance of the class (point to a separate block of memory for a separate class instance). I think you want the latter so your code would change for "bondList" and the other variables.

class Atom:
    def __init__(self,name, atNum):
        self.atomName = name
        self.atomicNum = atNum
        self.bondList = []

    def addBondedAtom(Atom):
        self.bondList.append(Atom)

You might also take a look at the style guide for variable name conventions http://www.python.org/dev/peps/pep-0008/

Edited 7 Years Ago by woooee: n/a

Comments
Clear, didactic and solved the issue.

Thank You for the replies guys. Just to clarify one point that arose.

So when I declared and instatiated theses variables

cartesian = dict()
bondList = list()
atomName = str()
atomicNum = int()

with out the 'self.' I'm truly instatiating 'STATIC' variables?

Abraço,
Jorge

Python does not have static variables, but does have global variables, which those are not. They are class objects and would be called using
Atom.cartesian
both inside and outside the class. In this simple example, note that cartesian belongs to the class and not the instance of the class:

class Atom:

    cartesian = 1

    def __init__(self):
        self.atom_name = "atom name"

    def print_cartesian(self):
        print "cartesian =", Atom.cartesian

print Atom.cartesian  ## --> 1
try:
    print Atom.atom_name  ## AttributeError: class Atom has no attribute 'atom_name'
except:
    print

Atom.cartesian += 1
x = Atom()
print Atom.cartesian
print x.atom_name
x.print_cartesian()
This question has already been answered. Start a new discussion instead.