help please

Steven Bethard steven.bethard at gmail.com
Sat Feb 12 19:43:09 EST 2005


Erik Max Francis wrote:
> gargonx at gmail.com wrote:
> 
>> UnboundLocalError: local variable 't2' referenced before assignment
> 
>     ...
> 
>> t2=""
>>
>> def Proc(text): # "text" is some random text or use OrigText
>>     ...
>
> The fix is to declare t2 global at the top of Proc:
> 
>     def Proc(text):
>         global t2
>         ...

But please don't.  The global declaration is a wart of Python, and in 
most cases, your code will be much cleaner if you avoid it.  Certainly 
in this case, making t2 a local of the function is much more sensible. 
If you stick with your current implementation (which I advise against), 
you should write it something like:

def proc(text):
     t2 = ""
     ...
     # your Proc code here
     ...
     return t2

Steve



More information about the Python-list mailing list