Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scope of a class..help???

18 views
Skip to first unread message

lokesh...@gmail.com

unread,
May 23, 2013, 5:51:36 AM5/23/13
to
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[i]
new_node.next = current_node
current_node = new_node
current_node.next = None
while Node:
print Node.data
Node = Node.next

Chris Angelico

unread,
May 23, 2013, 6:00:27 AM5/23/13
to pytho...@python.org
On Thu, May 23, 2013 at 7:51 PM, <lokesh...@gmail.com> wrote:
> 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

Peter Otten

unread,
May 23, 2013, 6:18:16 AM5/23/13
to pytho...@python.org
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"



lokesh...@gmail.com

unread,
May 23, 2013, 6:23:26 AM5/23/13
to
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?

lokesh...@gmail.com

unread,
May 23, 2013, 6:25:57 AM5/23/13
to
ok Peter Otten,
but how to make a Class global??

Chris Angelico

unread,
May 23, 2013, 6:27:27 AM5/23/13
to pytho...@python.org
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

Chris Angelico

unread,
May 23, 2013, 7:40:14 AM5/23/13
to pytho...@python.org
On Thu, May 23, 2013 at 8:25 PM, <lokesh...@gmail.com> wrote:
> 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

Tim Roberts

unread,
May 24, 2013, 1:05:57 AM5/24/13
to
lokesh...@gmail.com wrote:
>
>ok Peter Otten,
>but how to make a Class global??

Your class IS global. I still don't think you understand what you did
wrong. You've tripped into a strange little quirk of scoping. Here is
your code with some unimportant lines removes.

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

## OK, at this point, you have a global called "Node", which is a class.

def number_to_LinkedList(numbers):
pass
list_numbers = list(numbers)
head_node = Node()
# ...
current_node.next = None
while Node:
print Node.data

Python actually does a first scan through your function before it starts to
compile it. When it does the scan, it sees you use the name "Node" as a
local variable. At that point, it remembers that "in this function scope,
'Node' is a local name". Now, it codes to compile the class. When it
encounters Node() for the first time, it sees that "Node" is not defined
locally, even though it was supposed to be a local name.

You are assuming that the first Node() in that function will automatically
refer to the global class. It won't. The only way to solve this dilemma
is to change the name you use in the "while" loop.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
0 new messages