Why we will use obj$func() often

Donn Cave donn at drizzle.com
Sat Apr 24 13:29:53 EDT 2004


Quoth "Mark Hahn" <mark at prothon.org>:
| "Donn Cave" <donn at drizzle.com> wrote ...
...
|> I might seriously
|> consider what I believe we call lexical scoping, and a declaration -
|> not assigning a type, but an initial value, like "let x = 0".
|> Namespaces like Python's would be for objects - modules, classes,
|> class instances, whatever - not functions - and would be purely
|> explicit, not implicit like a scope (I think that would most
|> conspicuously require some alternative for "def" statements,
|> for example.)
|
| I'm not following you exactly, but I think what you are asking for is the
| declaration of variables with "let x = 0", or what some languages use is
| just "var x" that specify that the variable belongs in the scope of that
| declaration.  This resolves the ambiguity of local versus global variables
| and even gives a nice solution for closures.

The declaration is probably the most trivial part of it.  The notation
could be omitted - may as well, since "x = 0" would be implicitly the
the same as "let x = 0" anyway.  The point is that scope is purely
lexical, defined by source structure - top to bottom, left to right,
outside to inside.

My hunch is that this would be less confusing, especially multiple
nested scopes are no problem with lexical scoping.  It should also
make for more efficient execution, because compiler/interpreter is
looking at the complete story on what's what.  For example, you can
have true global constants, a common programming notion that Python
doesn't support.

It would be fatal for closures of the counter type, because that
takes a runtime binding somewhere.  This would be legal code -

   mkCounter = function ():
       i = 0
       countup = function ():
           i = i + 1
           print i
       return countup

... but the countup function would always print "1".  You'd need
an explicit function namespace, like -

   mkCounter = function ():
       countup = function ():
           .i = .i + 1
           print .i
       countup.i = 0
       return countup

... but maybe this is where we realize that this functionality
belongs to objects.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list