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
Ah.
You'll need to change your getAtomName function to the following: -
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
for various reasons which I can't remember off hand. I think this might make the global variables work better...
Good luck
4 4233
Ah.
You'll need to change your getAtomName function to the following: -
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
for various reasons which I can't remember off hand. I think this might make the global variables work better...
Good luck
bvdet 2,851
Expert Mod 2GB
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.
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...
Hello Guys, thank you for the replies. The problem was the 'self.' bit.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Phil Powell |
last post by:
class OptionsView extends PaginationView {
/**
* Constructor
*
* @access public
*/
function OptionsView() {} // CONSTRUCTOR
}
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |