Python object from dictionary

Sep 4, 2014

It is more convenient to access the fields of a class using the dot notation than accessing the entries in a dictionary. Python allows a quick conversion between simple dictionaries to python objects.

x = myObj.x
x = myDict['x']

A python object can be instantiated from the contents of a dictionary to take advantage of the dot notation. The following code snippet populates the internal member variable __dict__ of a Python object with the dictionary provided as a parameter to the constructor.

class myClass:
    def __init__(self, **entries): 

Each key of the dictionary becomes a member variables in the resulting myClass instance and its values is set to the corresponding value in the dictionary.

>>> d = {'a': 10, 'b': 12}
>>> c = myClass(d)
>>> print c.a