Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] Explicit Lexical Scoping (pre-PEP?)

Boris Borcicbborcic at gmail.com
Tue Jul 11 18:21:20 CEST 2006


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 incI 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 inUnboundLocalError: local variable 'num' referenced before assignmentWhat probably trips you is that the compiler thus makes a choice of interpretation that has no use cases.Cheers, BB


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2026 Movatter.jp