Scope of a class..help???

L

lokeshkoppaka

i had written the following code i am unable to create the instance of the class "Node" in the method "number_to_LinkedList" can any one help me how to do ??
and what is the error??


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



def number_to_LinkedList(numbers):
pass
list_numbers = list(numbers)
head_node = Node() #unable to create the instance saying UnboundedLocal
head_node.value = list_numbers[0]
head_node.next = None
current_node = head_node
for i in range(1,len(list_numbers)):
new_node = Node()
new_node.value = list_numbers
new_node.next = current_node
current_node = new_node
current_node.next = None
while Node:
print Node.data
Node = Node.next
 
C

Chris Angelico

i had written the following code i am unable to create the instance of the class "Node" in the method "number_to_LinkedList" can any one help me how to do ??
and what is the error??

It would really help if you post the actual exception and traceback.
It's UnboundLocal, not Unbounded... and here's the problem:
def number_to_LinkedList(numbers):
head_node = Node() #unable to create the instance saying UnboundedLocal
while Node:
print Node.data
Node = Node.next

You're assigning to Node. I think you mean to have some other local
variable here, for the iteration at the end. Since you assign to the
name Node inside the function (and don't have a global declaration),
the name Node is local to the function. Python doesn't let you
reference the global "prior to" shadowing it with the local, so you
get this error.

ChrisA
 
P

Peter Otten

i had written the following code i am unable to create the instance of the
class "Node" in the method "number_to_LinkedList" can any one help me how
to do ?? and what is the error??


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



def number_to_LinkedList(numbers):
pass
list_numbers = list(numbers)
head_node = Node() #unable to create the instance saying
UnboundedLocal head_node.value = list_numbers[0]
head_node.next = None
current_node = head_node
for i in range(1,len(list_numbers)):
new_node = Node()
new_node.value = list_numbers
new_node.next = current_node
current_node = new_node
current_node.next = None
while Node:
print Node.data
Node = Node.next


You have to decide if you want to use a name in a function locally or to
access a global. Python treats names that are being assigned to anywhere in
a function as local throughout the whole function.

x = "global"
def f():
print x # ok, access global variable

x = "global"
def g():
x = "local"
print x # ok, accessing local variable

x = "global"
def h():
print x # error, accessing local variable that has not
# been assigned a value
x = "local"
 
L

lokeshkoppaka

Thanks Chris Angelico,
i am new to python can you suggest me how to remove the error and solve it.
so,how can i create an instance for "Node" in that function??,is, it not possible to create an instance in such a way?
 
C

Chris Angelico

Thanks Chris Angelico,
i am new to python can you suggest me how to remove the error and solve it.
so,how can i create an instance for "Node" in that function??,is, it not possible to create an instance in such a way?

The problem isn't the instantiation; the problem's further down, where
you reuse the name "Node" to iterate over your list. Rename that to
something else and you should be right.

ChrisA
 
C

Chris Angelico

ok Peter Otten,
but how to make a Class global??

He gave some examples. It'd be helpful to quote some of his post, for
context... and preferably, show some proof that you've understood it.

You're starting a number of threads that look like you're trying to
get us to do your homework. You need to demonstrate that you actually
understand the material you're supposed to be learning.

ChrisA
 

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,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top