Why we will use obj$func() often

Donn Cave donn at u.washington.edu
Mon Apr 26 13:45:11 EDT 2004


In article <mailman.7.1082955858.25742.python-list at python.org>,
 "Mike C. Fletcher" <mcfletch at rogers.com> wrote:
...
> How about:
> 
>     .this (surrounding scope, often an object)
>     ..this (surrounding scope + 1, such as a module or a nested-class' 
> parent

Now this sounds like runtime namespace lookup.  Consider
  def g():
      print .x
  def f():
      x = 5
      g()

That prints 5, right?  Not, I suppose, but that's how object
"scope" works.  Nested function scope is at least to some extent
inherently lexical, the way we normally expect it to work - you're
referring to outer code blocks, not just any caller's namespace.

> Seems elegant in a certain way.  The construct is reminiscent of 
> directory specifiers, and meshes nicely with the attribute access 
> syntax.  Gets a little unwieldy if you're having hugely nested 
> constructs, but then that's already unwieldy, so more pain too them :) .

I still think elegant is not possible when this lexical notion
of scope is bolted onto a runtime namespace lookup system (dynamic
scope.)  (I cringe with fear when I use these terms, but I swear
I did look the Lisp definitions up.)

Consider
  def f():
      x = 5
      def g():
          print x
      x = 7
      return g
  t = f()
  t()

Does it print 5 or 7?  Of course, we know it's going to be 7,
because we understand this system and know how to use it, but
I'm saying this is perverse in terms of what people ought to
need to understand.  (I also think it's an unfortunate feature
to have to support, if it means preserving f()'s entire local
scope dictionary for the lifetime of t.)

But I admit my approach might not allow what I imagine people
would expect in places, either.  Suppose in the above, f invoked
g instead of returning it.  Then (I think) many would want x to
be 7 in g.  My `until a better idea comes along' response would
be `too bad, use a function parameter', and I can talk about the
value of this attitude towards programming, but I don't have
enough charisma to make it popular.  Similarly, I would certainly
not allow any function to rebind variables in the caller.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list