Why we will use obj$func() often

Bengt Richter bokr at oz.net
Tue Apr 27 16:00:38 EDT 2004


On Mon, 26 Apr 2004 15:10:22 +1200, Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:

>Mark Hahn wrote:
>> The whole point is to make things simpler, more readable, and more
>> efficient, but when people see the & symbol they somehow automatically think
>> it's complex.
>
>The feature itself might not be complex, but it's
>unfamiliar, since it's not quite like anything in any
>other language they're likely to be familiar with.
>So it makes learning the language a more complex
>exercise, since it's one more new thing to learn.
>
>It's also something that will be quite rarely used,
>and is totally non-suggestive of its meaning, so I
>expect people will be looking it up in the manual a
>lot before it finally gets internalised.
>
>On the other hand, replacing it with a keyword such
>as "outer" would make it highly suggestive, to the
>point where I'd wager most people wouldn't have to
>look it up at all.
>
>This is a big advantage that words have over
>punctuation -- you have much more scope for choosing
>one that means something to people.
>

The word "locals" already exists. If it returned a namespace
object instead of a dict proxy when given a nesting-level arg, we
could write

     def f():
         x = 123
         locals(0).x = 123 # synonym
         print x   # 123
         def g():
             x = 456
             print x  # 456
             print locals(1).x  # 123
             locals(1).x = 789
             locals(2).x = 101112
         print x  # 123
         g()	# 456 then 123
         print x  # 789
         g()    # 456 then 789

     print x # 101112

exec-ing or eval-ing 'locals(n)' could return a proxy object that would work
like the current proxy dict (i.e., a read-only snapshot except if it happened
to access globals()).

I suppose you could do a declarative form by giving global (or extern or outer)
a nesting level too.

Hoever, I think lhs := rhs as a spelling of find-and-rebind-lhs would cover
most use cases. (Requiring existing and lhs to have been defined first by
some lhs = rhs in the current or some lexically enclosing scope).

You couldn't reach locally shadowed outer variables of the same name, as you could
with locals(n).name, but how often do you really want to do that? Of course,
locals(n) and global/extern/outer(n) and := could be all be implemented.

Regards,
Bengt Richter



More information about the Python-list mailing list