namespace hacking question
showell30 at yahoo.com
Tue Oct 5 03:40:31 CEST 2010
On Sep 30, 10:07 am, kj <no.em... at please.post> wrote:
> This is a recurrent situation: I want to initialize a whole bunch
> of local variables in a uniform way, but after initialization, I
> need to do different things with the various variables.
I'm curious what a typical use case for this is. It seems funny to
have variables that are initialized the same way, yet which are not in
> What I end up doing is using a dict:
> d = dict()
> for v in ('spam', 'ham', 'eggs'):
> d[v] = init(v)
If you really want to get vars into the local namespace after the
initialization step, you can do something like this:
spam, ham, eggs = [init(v) for v in ['spam', 'ham', 'eggs']]
It's certainly awkward and brittle, but it lets you trade off re-
typing init() and d for another type of lexical duplication.
I wonder if you are either a) trying too hard to work around harmless
duplication or b) dealing with a duplication smell that actually runs
deeper than namespace hacking can fix.
More information about the Python-list