[Python-ideas] An illustrative example for PEP 3150's statement local namespaces

Nikolaus Rath Nikolaus at rath.org
Thu Oct 20 19:41:33 CEST 2011


Nick Coghlan <ncoghlan-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> writes:
> A task I actually ran into at work this week: get a filtered list of
> subdirectories, exclude some based on a list of names to be ignored,
> sort the remainder by their modification times.
[...]

Some unrelated feedback to that PEP (very neat idea, IMO):

1. Shouldn't:
,----
| # Current Python (manual namespace cleanup)
| def _createenviron():
|   ... # 27 line function
| 
| environ = _createenviron()
| del _createenviron
| 
| # Becomes:
| environ = _createenviron() given:
|     def _createenviron():
|       ... # 27 line function
`----
really be      
,----
| 
| # Becomes:
| environ = _environ given:
|     ... # 27 line function that defines _environ
`----
What's the point of defining a function in the given clause if you only
execute it once? You may just as well run the function code directly in
the given clause.

2. In
,----
| # Current Python (early binding via default argument hack)
| seq = []
| for i in range(10):
|   def f(_i=i):
|     return i
|   seq.append(f)
| assert [f() for f in seq] == list(range(10))
`----
You probably meant "return _i"?

3. In my opinion the explicit early binding syntax is very ugly. Also,
doesn't it restrict early binding to just one variable?

4. I think having given blocks default to early binding and other nested
scopes not is very counterintuitive (but I don't have any idea of how to
best resolve this).



Best,

   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Python-ideas mailing list