Function to execute only once

Paul Rubin http
Fri Oct 14 17:21:25 EDT 2005


"snoe" <case.nelson at gmail.com> writes:
> Also why is it if I set tmp as a global and don't pass it as a
> paremeter to the various functions as per the OP that I get an
> "UnboundLocalError: local variable 'tmp' referenced before assignment"?

If you don't declare it as a global, and if you try to assign a value
to it, then Python thinks it's a local.  If you only refer to it and
never assign it, Python decides it's global.  Python is weird that
way.  One consequence is that if it's local to some outer scope, then
it's neither global nor local to your function, so there's no way to
assign to it.

I think Pythonic style is to not do complex things with closures,
but to use class instances instead.



More information about the Python-list mailing list