scopes of local and global variable

Fredrik Lundh fredrik at pythonware.com
Fri Dec 22 18:55:32 EST 2006


Pyenos wrote:

> #############################CODE##############################
> t_len=0
> class WORK:
>     def getwork(self):
>         def formattable(table_to_process,type):                                               
>             TYPE=["p","t","T","s","i"] #list of types to format
>             if type==TYPE[1]:
>                 def format_t():
>                     row=[]
>                     for col in table_to_process:
> 
>                         #######################
>                         # ERROR PRONE PART    #
>                         #######################
>                         if len(str(col))>t_len:
>                             t_len=len(str(col))
>                         #######################
> # Error message says:                                                   #
> # UnboundLocalError: local variable 't_len' referenced before assignment#
> 
>                         row+=col
>                         if (table_to_process.index(col)+1)%7==0:
>                             t_temp.append(row)
>                             row=[]
>                 format_t()
> #################################################################

wow.

> Interpreter says that t_len is local variable although i have
> specified t_len=0 in line 1. Also, although i've stated t_len=0 in
> line 1, it says that t_len is referenced before assignment.

each function introduces a new scope.

</f>




More information about the Python-list mailing list