Fredrik Lundh wrote:
> Just van Rossum wrote:
> 
>> Why couldn't at least augmented assignment be implicitly rebinding? It
>> has been suggested before (in the context of a rebinding operator), but
>> I'm wondering, is this also off the table?
>>
>>     def counter(num):
>>         def inc():
>>             num += 1
>>             return num
>>         return inc
>>
>> Reads very natural to me. It's likely the most frequent example of what
>> people try before they learn that rebinding to outer scopes isn't
>> allowed. It could Just Work.
> 
> note that most examples of this type already work, if the target type is 
> mutable, and implement the right operations:
> 
>       def counter(num):
>           num = mutable_int(num)
>           def inc():
>               num += 1
>               return num
>           return inc

I agree with you (and argued it in "scopes vs augmented assignment vs sets" 
recently) that mutating would be sufficient /if/ the compiler would view 
augmented assignment as mutations operators : which it doesn't as far as 
concerns scopes where a variable appears as target only of /augmented/ 
assignments.

Currently, the code you propose above will not work, and whatever your 
mutable_int() it will result in

UnboundLocalError: local variable 'num' referenced before assignment

What probably trips you is that the compiler thus makes a choice of 
interpretation that has no use cases.


Cheers, BB

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to