Carrying variables over from function to function
bruno modulix
onurb at xiludom.gro
Tue Sep 27 08:23:50 EDT 2005
Peter Otten wrote:
> Bruno Desthuilliers wrote:
>
>
>>2/ functional solution:
>>-----------------------
>>def make_funcs():
>> x = 0
>> def _abc():
>> x = 1
>> return x + 1
>> def _abcd():
>> return x + 1
>> return _abc, _abcd
>>
>>abc, abcd = make_funcs()
>>print abc()
>>print abcd()
>
>
> The x in function _abc() is not the same as that in make_funcs() and _abcd()
> as you can easily verify by modifying _abc() to
>
> def _abc():
> x # raises UnboundLocalError
> x = 1
> return x + 1
>
> Once a variable is assigned a value the compiler treats it as local to that
> function. Closed-over variables are therefore always read-only, much to the
> chagrin of Lisp-lovers.
Doh :(
I wasn't aware of this limitation (I thought I had done this before in
Python and it was working, but it seems that I suffer a severe case of
MemoryCorruption...).
And this will also teach me to *always* test my code before posting (I
usually do...).
<op>
Sorry, my bad :(
</op>
<peter>
Thanks for the correction
</peter>
--
bruno desthuilliers
ruby -e "print 'onurb at xiludom.gro'.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list