Namespace hack
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu May 24 22:47:11 EDT 2012
Pardon me for breaking threading, but Daniel's response is not available
on my ISP's news server, and I only discovered it by accident.
On Thu May 24 15:04:34 CEST 2012, Daniel Fetchinson <fetchinson at
googlemail.com> wrote:
> > On Thu, 24 May 2012 08:50:59 +0000, Steven D'Aprano wrote:
> > From the Zen of Python ("import this"):
> > Namespaces are one honking great idea -- let's do more of those!
[...]
> Funny, you got to the last line of "import this" but apparently
> skipped the second line:
>
> Explicit is better than implicit.
>
> And you didn't even post your message on April 1 so no, I can't laugh
> even though I'd like to.
Do you object to the ability to write standard Python modules?
# module.py
def spam(obj, n):
return len(obj) + n
def ham(obj):
return spam(obj, 23)
By your apparent misunderstanding of the Zen, you think that this should
be written with oodles of more explicitness, 'cos explicit is always
better, right?
keyword.def globals.spam(locals.obj, locals.n):
keyword.return builtin.len(locals.obj) + locals.n
keyword.def globals.ham(locals.obj):
keyword.return globals.spam(locals.obj, 23)
Python, like most (all?) non-trivial languages, has scoping rules so that
you can refer to names without explicitly specifying which namespace they
are in. So long as this is unambiguous, Explicit vs Implicit is
irrelevant if not outright wrong.
My namespace decorator simply applies a slightly different set of scoping
rules to the ones you are already used to in modules. It's no worse than
nested functions (hardly a surprise, because it is built on nested
functions!) or module-level scoping rules.
--
Steven
More information about the Python-list
mailing list