[IPython-dev] RFC on policy: setting object attributes?

Fernando Perez fperez.net@gmail....
Wed Dec 30 16:00:00 CST 2009


Howdy,

as I dive into Brian's wonderful refactor now merged in trunk, I
realize we have now a fair amount of code with objects that set
attributes in methods other than __init__.  I'm a big fan of declaring
all attributes an object can have either in the class declaration or
in __init__, so that one can read the beginning of the class and know
exactly what to expect later on from that object.  Having attributes
appear later from method calls requires either using try/except or
lots of getattr() calls to avoid possible AttributeErrors.

So my question is: should we in general set these in the class or in
__init__? For code where we use traits-like tools (either traitlets or
Traits) obviously things must be put in the class, but for other
attributes we can choose.  I see advantages to putting these in the
class:

- uniformity (if something becomes traits-like later, no need to
change where it's declared)
- the cost of that execution is paid only once per class declaration,
not every time an instance is created.

As a minus, import times are likely to be *slightly* larger, as class
declarations have extra code in them.

Opinions?  For now I'm going to go with putting things in the class as
I need them, but I'd like to hear people's thoughts so we have a
policy we're happy with and good reasons to choose it.

Cheers,

f


More information about the IPython-dev mailing list