changing local namespace of a function
Alex Martelli
aleaxit at yahoo.com
Sat Feb 5 04:37:10 EST 2005
Bo Peng <bpeng at rice.edu> wrote:
> M.E.Farmer wrote:
> > I really don't see your need.
>
> Maybe it is just my laziness. It is almost intolerable for me to write
> lines and lines of code like
>
> d['z'] = func(d['x']+d['y']+d['whatever']['as']+d[a][0] )
>
> It is ugly, unreadable and error prone. If I have to use this code, I
> would write
>
> _z = func(_x + _y + _whatever['as'] + _a[0])
So, what about having as the real code
d.z = func(d.x + d.y + d.whatever['as'] + d.a[0])
Doesn't seem too different from what you would write, would it?
To wrap a dictionary into an object whose attributes are mapped to items
of the dictionary is not hard: just start your function with
d = type('wrapadict', (), {})()
d.__dict__ = the_dict_to_wrap
and there you are.
Affecting *BARE* names is a completely different order of magnitude of
complication away from affecting *COMPOUND* names. Barenames are,
almost inevitably, handled with deep magic, essentially for the purposes
of can't-do-without optimization; compound names are _way_ more
malleable...
Alex
More information about the Python-list
mailing list