Easy "here documents" ??
Fredrik Lundh
fredrik at pythonware.com
Sun Dec 19 13:48:06 EST 2004
Steve Holden wrote:
>> here's a rather horrid piece of code that turns a list of dictionaries
>> into a single object [that] 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.
you can have a lot more fun with "class" than with "def"...
> 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 innermost scopes.
except that if you do that, changes to the individual dictionaries won't
be visible in the "flattened" view.
</F>
More information about the Python-list
mailing list