Real Problems with Python

Michael Hudson mwh21 at cam.ac.uk
Sun Feb 13 14:08:16 EST 2000


=?ISO-8859-1?Q?Fran=E7ois_Pinard?= <pinard at iro.umontreal.ca> writes:

> "Tim Peters" <tim_one at email.msn.com> writes:
> 
> > > 2. Lack of lexical scoping
> > >
> > >    Tim Peters disagrees, but I miss it a lot, even after using Python
> > >    for years.
> 
> > Actually, Tim wholly agrees that you miss it a lot.
> 
> Couldn't we approximate lexical scoping with classes?  For the few times I
> needed it, I was fairly satisfied with this solution.  Surely more verbose
> than in Scheme, but yet, given I do not use it often, I did not care the
> extra-verbosity.

This is what I fairly often recommend to people who are abusing the
default argument hack, or slate Python for not doing scoping properly.
People still complain though.

It would also be nice if people got the terminology right; Python *is*
lexically scoped, because references to a name can only appear within
code that is textually contained in the establishing contruct (says he
paraphrasing from cltl2). The alternative is the poorly named "dynamic
scope" which would mean things like:

def f():
    a = 1
    return h()

def g():
    a = 1
    return h()

def h():
    return a

print f(),g()

would print "1 2", which is just not true. This may seem bizarre, but
it can be useful; if you ever find yourself adding arguments to
functions only so you can propogate said options down to functions you
then call, you might have been better off with a dynamically scoped
variable (they're called "special" in common lisp). They're a bit like
acquisition in some ways. They also monumentally don't fit into the
Python way of doing things, so I'm not going to press for them.

The "problem" is that scopes don't nest - except for the scopes of
instance members, so use them instead.

Cheers,
Michael



More information about the Python-list mailing list