By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,201 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,201 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Expand|Select|Wrap|Line Numbers
  1. <Atom.Atom object at 0x7f0b09597fd0>
  2. Traceback (most recent call last):
  3.   File "./Main.py", line 7, in <module>
  4.     print (t.getAtomName())
  5.   File "/home/jorge/Documentos/projetos/mestrado/códigos/cartesian_zmatrix/Atom.py", line 22, in getAtomName
  6.     return atomName
  7. NameError: global name 'atomName' is not defined
  8.  
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:

Expand|Select|Wrap|Line Numbers
  1.  #! /usr/bin/python3
  2.  
  3. from Atom import *
  4.  
  5. t = Atom("C",12)
  6. print (t.getAtomName())
  7.  
Expand|Select|Wrap|Line Numbers
  1. class Atom:
  2.  
  3.     cartesian = dict()
  4.     bondList = list()
  5.     atomName = str()
  6.     atomicNum = int()
  7.  
  8.     def __init__(self,name, atNum):
  9.         self.atomName = name
  10.         self.atomicNum = atNum
  11.  
  12.     def setCoordinates(x, y, z):
  13.         catesian['x'] = x
  14.         catesian['y'] = y
  15.         catesian['z'] = z
  16.  
  17.     def addBondedAtom(Atom):
  18.         bondList.append(Atom)
  19.  
  20.     def getAtomName():
  21.         return atomName
  22.     def getAtomicNumber():
  23.         return atomicNum
  24.     def getBondList():
  25.         return bondList
  26.     def getCartesian():
  27.         return cartesian
  28.  
I appreciate any help, and also any tip you might have considering I've just started with python.

Abraço,
Jorge
Nov 19 '09 #1

✓ answered by Glenton

Ah.

You'll need to change your getAtomName function to the following:
Expand|Select|Wrap|Line Numbers
  1.      def getAtomName(self):
  2.          return self.atomName
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. class Atom(object):
  2.  
for various reasons which I can't remember off hand. I think this might make the global variables work better...

Good luck

Share this Question
Share on Google+
4 Replies


Expert 100+
P: 391
Ah.

You'll need to change your getAtomName function to the following:
Expand|Select|Wrap|Line Numbers
  1.      def getAtomName(self):
  2.          return self.atomName
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. class Atom(object):
  2.  
for various reasons which I can't remember off hand. I think this might make the global variables work better...

Good luck
Nov 19 '09 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 19 '09 #3

Expert 100+
P: 391
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...
Nov 19 '09 #4

P: 2
Hello Guys, thank you for the replies. The problem was the 'self.' bit.
Nov 19 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.