[Python-au] strange pre-acting bug

PeterL pacqa100 at yahoo.com.au
Tue May 18 23:09:58 UTC 2010


Interesting. This is a common mis-understanding on the scoping rules of 
Python functions. I might do a lightning talk on it at PyCon.
You need to be clear on the error. An 'UnboundLocalError' means that you 
are using a mainline variable inside a function before you change it.

In Python, a function *can* _read_ a mainline's variable with no problem.
If a function goes to change a mainline's variable, then it become 
'bound local', which means you must give it a value before you use it.
Hope this helps

Compare:
-----------
# Mainline variable available to a fn
def fn():
     print x    # Prints 5

x = 5
fn()
----------
# Mainline variables can be localised inside a fn
def fn():
     x = 7
     print x    # Prints 7
     # Throw the localised value of x away

x = 5
print x   # Prints 5
fn()
print x   # Prints 5
----------
# Local bound variables must be set before they're used
def fn():
     print x    # Error - UnboundLocalError - mainline variables that 
are localised must be set before used
     x = 7     # Localise x
     print x    # Never reaches here

x = 5
fn()
------------


-- 
Peter Lovett
----------------------------------------------------------------------
Plus Plus Pty Ltd
training + consulting + development
C + C++ + C# + Perl + Python + Java + VB/VBA/ASP + SQL + HTML + XML
Unix&  Shell scripting
Microsoft&  Adobe products

http://www.plusplus.com.au/
http://www.pythontraining.com.au/

Everything should be made as simple as possible, but not simpler.
-- Einstein
----------------------------------------------------------------------

On 19/05/2010 7:22 AM, Garry Trethewey wrote:
> Hello.
> I've just joined this list because I've got a bug that I can't 
> understand. AFAIK it shouldn't be able to happen, and I hope someone 
> can enlighten me. I don't even know what to call it to google it.
>
> Briefely, if I comment out a line, a line _before_ it works, if I 
> enable the line, the line before it doesn't work.
>
>
> 212  print 'vPage = ', vPage
> 213  raw_input('press enter')
> 124  '''
> 125  if vPage.isdigit():
> 126    vPage = str(int(vPage) + 1)
> 217    '''
> 218  print 'vPage = ', vPage
> 219  raw_input('press enter')
>
>
> vPage =
> press enter
> vPage =
> press enter
>
>
> but if I uncomment 214 - 217
>
> 212  print 'vPage = ', vPage
> 213  raw_input('press enter')
> 124
> 125  if vPage.isdigit():
> 126    vPage = str(int(vPage) + 1)
> 217
> 218  print 'vPage = ', vPage
> 219  raw_input('press enter')
>
>
> vPage =
> Traceback (most recent call last):
>   File "/home/garry/Desktop/bimbo/script/buggy_intact/get_scan02.py", 
> line 381, in <module>
>     vPage =fn_get_page_num()
>   File "/home/garry/Desktop/bimbo/script/buggy_intact/get_scan02.py", 
> line 212, in fn_get_page_num
>     print 'vPage = ', vPage
> UnboundLocalError: local variable 'vPage' referenced before assignment
>
>
> While I'm sure I could find a workaround for this instance, I'm more 
> interested in the fact that as far as I can se, this bug should be 
> impossible.
>
> Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)
> [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
>
> Ubuntu 8.04
>
> I'm happy to provide all my code. The whole project is < 1000 lines 
> plus  easygui.py from http://easygui.sourceforge.net/ if anybody wants 
> a look.
>
>
>
> regards
> ------------------------------------
> Garry Trethewey
> ------------------------------------
>
>
>
> _______________________________________________
> python-au maillist  -  python-au at starship.python.net
> http://starship.python.net/mailman/listinfo/python-au
>





More information about the python-au mailing list