Scope of a class..help???

Chris Angelico rosuav at gmail.com
Thu May 23 06:00:27 EDT 2013


On Thu, May 23, 2013 at 7:51 PM,  <lokeshkoppaka at 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



More information about the Python-list mailing list