Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and dictionary like object

I need a python 3.1 deep update function for dictionaries (a function that will recursively update child dictionaries that are inside a parent dictionary).

But I think, in the future, my function could have to deal with objects that behave like dictionaries but aren't. And furthermore I want to avoid using isinstance and type (because they are considered bad, as I can read on almost every Pythonista's blog).

But duck typing is part of Python's philosophy, so how could I check if an object is dictionary-like?

Thanks!

Edit : Thank all for the answers. Just in case, the function I coded can be found at this place : http://blog.cafeaumiel.com/public/python/dict_deep_update.py

like image 497
thomas Avatar asked Aug 14 '09 13:08

thomas


People also ask

Are dictionaries like objects in Python?

Python provides another composite data type called a dictionary, which is similar to a list in that it is a collection of objects.

Can you make a dictionary of objects in Python?

Python's dictionary allows you to store key-value pairs, and then pass the dictionary a key to quickly retrieve its corresponding value. Specifically, you construct the dictionary by specifying one-way mappings from key-objects to value-objects. Each key must map to exactly one value, meaning that a key must be unique.

What type of objects can be used as is in dictionaries?

only immutable type of objects like string, tuple or integer etc.

Can dictionaries contain objects?

The dictionary stores objects as key-value pairs and can be used to represent complex real-world data.


3 Answers

Check out the (new in 3.x) abstract base classes (ABC's) in the collections module:

http://docs.python.org/3.1/library/collections.html

I would consider checking with isinstance against Mapping like in the following:

>>> import collections
>>> isinstance({},collections.Mapping)
True

Then, if you make your own dict-like class, make collections.Mapping one of its bases.

The other route is trying and catching whatever exceptions for the dictionary operations, - but with the recursion you're talking about, I'd rather check against the base type first than handle figuring out what dict or subdict or other dict member was or was not there to throw an exception.

Editing to add: The benefit of checking against the Mapping ABC instead of against dict is that the same test will work for dict-like classes regardless of whether or not they subclass dict, so it's more flexible, just in case.

like image 132
Anon Avatar answered Oct 13 '22 21:10

Anon


use isinstance, there is nothing wrong with it and it's routinely used in code requiring recursion.

If by dictionary-like you mean the object's class inherit from the dict, isinstance will also return True.

>>> class A(dict):
    pass

>>> a = A()
>>> isinstance(a, dict)
True
like image 26
SilentGhost Avatar answered Oct 13 '22 21:10

SilentGhost


Duck typing is where you do what you want to do, and deal with the fallout if your objects don't behave the way you expected them to.

You want to check if something is dict-like, and update it if it is, right? Just call the object's update method, and handle the Exception you get if there is no such method.

Of course, this will fall flat if you deal with custom class objects which have update methods that do something else entirely -- I'm not quite sure how to deal with that.

like image 42
sykora Avatar answered Oct 13 '22 23:10

sykora