16

I have a class that inflates objects from rows found in a database (or another source, e.g. MongoDB, a CSV file, etc.). To set the object's properties, it does something like self.__dict__.update(**properties) or obj.__dict__.update(**properties).

Is this considered Pythonic? Is this a good pattern that I should continue to use, or is this considered bad form?

2

1 Answer 1

11

In Python 3.3, a new type was added, types.SimpleNamespace(), and in the documentation it is described thus:

The type is roughly equivalent to the following code:

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

Note the __init__ method of the type; you cannot get a better endorsement of the technique than the Python documentation.

3
  • Though SimpleNamespace is quite different from most types in that it doesn't have a fixed set of attributes.
    – user7043
    Commented Aug 12, 2013 at 19:56
  • 7
    Well, speaking of the Python documentation, from docs.python.org/2/library/stdtypes.html "A special attribute of every module is __dict__. This is the dictionary containing the module’s symbol table. Modifying this dictionary will actually change the module’s symbol table, but direct assignment to the __dict__ attribute is not possible (you can write m.__dict__['a'] = 1, which defines m.a to be 1, but you can’t writem.__dict__ = {}). Modifying __dict__ directly is not recommended," which is what initially prompted me to ask this question.
    – skyler
    Commented Aug 12, 2013 at 20:11
  • @skyler: note that that only mentions the module namespace. The globals() function returns the same namespace, and there are usually better ways to solve problems than setting globals dynamically, hence the warning. Commented Mar 14, 2015 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.