Nested scopes resolution -- you can breathe again!

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sun Feb 25 13:11:20 EST 2001


Sun, 25 Feb 2001 10:54:21 -0600, Russell Turpin <rturpin at do.not.use> pisze:

> The wildcard feature on the "from" statement is a mistake, and
> should be avoided.

I agree. This doesn't conflict with my point.

Look this way: a name has not been explicitly hidden in an inner scope,
so it should not be silently made unavailable. Explicit is better than
implicit, so please don't hide names which were not explicitly shadowed
(i.e. assigned to).

Hiding as in older versions of Python leads to obscure bugs when the
default argument hack is forgotten, and more ugly code.

This hack is also not general, because you can't have a variable
number of arguments:
    lambda x, y, var=var, *rest: ...reference var here...
is not a replacement for
    lambda x, y, *rest: ...reference var here...

> Using either (a) or (b), anyone reading the code can easily determine
> which variables are non-local, and where they originate.

A good practice is to not shadow names from outer scopes explicitly.
In this case nested scopes make it easier to find where a name is
coming from: just spot *any* assignment to that name, and it is
defined there.

Python's practice does not create deeply nested scopes anyway,
so this is the smallest problem in finding variable's definition.

> One of the problems with wildcard imports is the risk of accidental
> private name collision, where an imported module uses the same
> private name as the one that imports it.
> 
> Nested scoping essentially is a wildcard import from all containing
> scopes.

You shouldn't show a disadvantage of wildcard imports and tell that
nested scopes are like wildcard imports, because nested scopes don't
have this problem! Names which you define are not shadowed by names
"imported" from nested scopes.

PS. A construction similar to wildcard import is not so bad where the
language protects from using ambiguous names (imported from different
modules or imported from a module and defined locally), e.g. as in
Haskell. They are useful especially when the language uses toplevel
functions where Python uses methods. I agree that it's bad in Python.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list