import help

P

placid

Hi all,

I have the following class code for a Node for a Linked List, (saved in
node.py)


class Node :
def __init__(self,element):
self.element = element
self.next = None


then i have another module called LinkedList.py, with following code

import sys
import node

def main():
print "Does nothin ATM, as it doesnt work"

main()


the problem im having is that im getting NameError exception when i run
LinkedList.py "global name 'Node' is not defined"

Does anyone know what the problem is here?

Thanks in advance
 
S

Scott David Daniels

placid said:
I have the following class code for a Node for a Linked List, (saved in
node.py)

class Node :
def __init__(self,element):
self.element = element
self.next = None

then i have another module called LinkedList.py, with following code

import sys
import node
def main():
print "Does nothin ATM, as it doesnt work"
main()

the problem im having is that im getting NameError exception when i run
LinkedList.py "global name 'Node' is not defined"

Does anyone know what the problem is here?

I have trouble believing you. I suspect you are telling us what you
think is happening, and leaving something vital out. In particular,
cut and paste the traceback.

Here's another experiment, interactively do:

import node
print node.__file__

Perhaps that will tell you something.

--Scott David Daniels
(e-mail address removed)
 
P

placid

Scott said:
I have trouble believing you. I suspect you are telling us what you

im not leaving anything out, im telling you what's happening
think is happening, and leaving something vital out. In particular,
cut and paste the traceback.

here it is!

Traceback (most recent call last):
File "LinkedList.py", line 7, in ?
main()
File "LinkedList.py", line 4, in main
n = Node("test")
NameError: global name 'Node' is not defined

Here's another experiment, interactively do:

import node
print node.__file__ node.py


Perhaps that will tell you something.

More information, im using Windows XP Pro, these two files are in same
directory...
 
P

placid

so there is a record of this problem i had, to solve this problem
either use

import node
.
.
.
n = node.Node("test")

or


from node import *
.
.
.
n = Node("test")



Cheers
 
D

Dennis Lee Bieber

Traceback (most recent call last):
File "LinkedList.py", line 7, in ?
main()
File "LinkedList.py", line 4, in main
n = Node("test")
NameError: global name 'Node' is not defined
That is NOT the same code as you posted.

You posted:
import sys
import node

def main():
print "Does nothin ATM, as it doesnt work"

main()

Line 4 of that is "def main():"

If you have any .pyc or .pyo files in the same directory as your
source, delete them, try again, and show us what happened.

BTW: as for the code that is being run... IT NEEDS

n = node.Node("test")

You imported a module "node", but the class definition named "Node"
is INSIDE the module -- so you have to specify
module.class
to access it...
--
 
P

placid

Dennis said:
That is NOT the same code as you posted.

You posted:
import sys
import node

def main():
print "Does nothin ATM, as it doesnt work"

main()

Line 4 of that is "def main():"

If you have any .pyc or .pyo files in the same directory as your
source, delete them, try again, and show us what happened.

BTW: as for the code that is being run... IT NEEDS

n = node.Node("test")

You imported a module "node", but the class definition named "Node"
is INSIDE the module -- so you have to specify
module.class
to access it...
--


Sorry, i left some code out, my bad, LinkedList.py contains (well it
doesnt anymore, but originally did)

import node

def main():
n = Node("Test")
print n.element

main()


but now i know why this doesnt work and the solution (see previous post
of mine)


thanks for all the help!
 
D

Dennis Lee Bieber

so there is a record of this problem i had, to solve this problem
either use

import node
.
.
.
n = node.Node("test")
This is the recommended method... It keeps module contents inside
the module

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,329
Latest member
InezZ76898

Latest Threads

Top