Generic dictionary
Anny Mous
b1540457 at tyldd.com
Sun Nov 20 05:46:25 EST 2016
On Sun, 20 Nov 2016 08:43 pm, Peter Otten wrote:
> Thorsten Kampe wrote:
>
>> [Crossposted to tutor and general mailing list]
>>
>> Hi,
>>
>> I'd like to extend the dictionary class by creating a class that acts
>> like a dictionary if the class is instantiated with a dictionary and
>> acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
>> instantiated with a list (that is dictitem).
[...]
> def GenericDict(dict_or_items):
> if isinstance(dict_or_items, dict):
> return dict(dict_or_items)
> else:
> return SimpleGenericDictWithOnlyTheFalseBranchesImplemented(
> dict_or_items
> )
Personally, I'd go even simpler:
dict(dict_of_items)
will return a dict regardless of whether you start with another dict or a
list or tuples. And then you just work on it as if it were a dict (which is
exactly what it actually is). And if you want to turn it back into a list
of (key, value) pairs, then you call:
list(d.items()) # Python 3
d.items() # Python 2
--
Steve
More information about the Python-list
mailing list