473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

4 4233
Glenton
391 Expert 256MB
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
2,851 Expert Mod 2GB
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
Glenton
391 Expert 256MB
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
Hello Guys, thank you for the replies. The problem was the 'self.' bit.
Nov 19 '09 #5

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

Similar topics

1
by: Phil Powell | last post by:
class OptionsView extends PaginationView { /** * Constructor * * @access public */ function OptionsView() {} // CONSTRUCTOR }
0
by: D.C. | last post by:
Hi, I am running into the above exception with different dll name every time I restart to debug the ASP.NET project. I put all my dlls in \bin directory. The exception happens on the...
3
by: Dave | last post by:
I've run across a strange error when creating a simple ADO.Net connection in VB.Net 2003 To replicate an error: Create an Access database (2002) with table named "Student" and fields SID...
4
by: J Rice | last post by:
I have been experimenting with some thread programming, but as I'm doing this on my own I am worried I might be making a major mistake. Here's a brief rundown of what I am working on. Multiple...
1
by: NachosRancheros | last post by:
Ok so I just started to program with Python about a week ago and I am trying to make a program that will take the path of a file and a shortcut command and save it to a text file. Eventually I want...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
6
by: newholborn | last post by:
Hi everyone. I have some questions, which should be rather easy to reply, but as I am working on PHP, JavaScript, XML, CSS, Photoshop and other stuff at the same time, my head, which is about to...
2
by: jmike | last post by:
I'm using some legacy code that has a user-defined exception in it. The top level program includes this line from TestRunError import * It also imports several other modules. These other...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.