[Python-au] strange pre-acting bug

Chris Miles miles.chris at gmail.com
Wed May 19 00:40:42 UTC 2010


On 19/05/2010, at 9:40 AM, Ryan Kelly wrote:

> On Wed, 2010-05-19 at 09:09 +1000, PeterL wrote:
>> 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.
> 
> It might also be instructive to look at the bytecode that python
> generates for each of these cases (or it might be that I just love
> looking at bytecode...)

Wow, that is actually really handy.  I never really considered examining the Python bytecode when debugging odd behaviour.  I certainly don't drop down to the assembly code when my C program crashes (maybe I should).

Motto of the day: Bytecode is your friend!

Cheers,
Chris




More information about the python-au mailing list