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?

asked Aug 12, 2013 at 18:53
2
  • 1
    I don't know if it's Pythonic, but it's certainly more common to do it in dunder init. Commented Aug 12, 2013 at 19:05
  • Cf. Is self.__dict__.update(**kwargs) good or poor style? on Stack Overflow. Commented Aug 12, 2013 at 20:34

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.

answered Aug 12, 2013 at 19:49
3
  • Though SimpleNamespace is quite different from most types in that it doesn't have a fixed set of attributes. 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. 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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.