[Python-Dev] Explicit Lexical Scoping (pre-PEP?) (original) (raw)
Boris Borcic bborcic at gmail.com
Tue Jul 11 18:21:20 CEST 2006
- Previous message: [Python-Dev] Explicit Lexical Scoping (pre-PEP?)
- Next message: [Python-Dev] Explicit Lexical Scoping (pre-PEP?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 = mutableint(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
- Previous message: [Python-Dev] Explicit Lexical Scoping (pre-PEP?)
- Next message: [Python-Dev] Explicit Lexical Scoping (pre-PEP?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]