Scope of a class..help???

Peter Otten __peter__ at web.de
Thu May 23 06:18:16 EDT 2013


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??
>   
> 
> 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

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"






More information about the Python-list mailing list