user-supplied locals dict for function execution?

Lonnie Princehouse finite.automaton at gmail.com
Mon Mar 20 15:46:15 EST 2006


Occaisionally, the first two lines of The Zen of Python conflict with
one another.

An API I'm working on involves a custom namespace implementation using
dictionaries, and I want a pretty syntax for initializing the custom
namespaces.  The fact that these namespaces are implemented as
dictionaries is an implementation detail, and I don't want the users to
access them directly.  I find the "implicit update" syntax to be much
cleaner:

@namespace   # indicates function should be executed in namespace
def initialize_namespace():
  x = 5

# versus the alternative

__namespace__ = {
  'x' : 5,
}


Of course, "x = 5" is a trivial example.  The contents of these
namespaces will probably contain on average dozens of items; they are
analagous to a module's global dictionary.  Most people probably prefer
to specify a module's globals with regular statements:

  x = 5

... instead of explicit dictionary assignment:

  __dict__['x'] = 5




More information about the Python-list mailing list