Easy "here documents" ??
steve at holdenweb.com
Sun Dec 19 19:36:49 CET 2004
Fredrik Lundh wrote:
> Jerry Sievers wrote:
>>It gets uglier though if you want to do this from inside a function
>>and have variables from more than one scope interpolated. For that
>>you need something that can treat a series of dicts as one.If there's
>>built in functionality in Python for this, I haven't discovered it
> you use them all the time: plain old instance objects...
> here's a rather horrid piece of code that turns a list of dictionaries
> into a single object the automatically searches the dictionary chain
> when you ask for attributes (use getattr for non-standard keys).
> def flatten_dicts(*dicts):
> root = None
> for dict in dicts:
> class wrapper: pass
> if root:
> wrapper.__bases__ = (root,)
> wrapper.__dict__ = dict
> root = wrapper
> return wrapper()
> obj = flatten_dicts(d1, d2, d3)
Iterative subclassing, yet. Yerch :-) It's sometimes amazing just how
down and dirty Python will let you get.
Of course, if the mappings were all dictionaries then it would be rather
simpler to just update an empty dict with the outermost through to the
though-efficiency-would-be-determined-by-usage-ly y'rs - steve
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
More information about the Python-list