I currently toying with Python 3.1 and I need a deep update function for dictionaries (a function that willl 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 isinstance
and type
are bad, as I can read on almost every Pythonista's blog.
But duck typing is part of Python's philosophy right? So how could I check if an object is dictionary-like? This way my function will be (I hope) Pythonic and quite powerful and will fit exactly my needs :o).
Note that even if I can find already coded deep update functions with Google, I believe coding my own is a part of my learning (and my first attempt to write true Python code).
Thanks!
PS - Feel free to tell me how I'm wrong on any point, I believe that's too a part of my learning!
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