[Twisted-Python] 3 basic questions about connectionLost, reactor stop and design

Andrew Bennetts andrew-twisted at puzzling.org
Mon Apr 2 08:58:08 EDT 2007


甜瓜 wrote:
[...]
> 
> Finally, my question is why twisted use class attribute so widely? Is
> there any benefit? In my opinion, __init__ function is the only good
> place to define 'attributes'. But
> 
> twist distributes 'attributes' in two forms: class and __init__. I
> want to know the idiom to decide which attribute should be put into
> which part.

No particular reason, but here are some small reasons for using class
attributes:

   * their presence (and initial value) is visible in the declaration of the
     class, rather than requiring reading the code of the __init__ method;
   * as a result, the variables (and initial values) will be automatically
     listed by tools like epydoc and pydoctor (if the author forgets to
     explicitly describe these attributes in the docstrings);
   * they require less typing from the code author than an assignment to
     "self.foo" in __init__;
   * if the class doesn't have an __init__ method yet, it saves even more typing
     to use a class variable than to add an __init__ just to set an instance
     variable;
   * they slightly reduce memory consumption.

I'm not sure these reasons are good enough to compensate for the confusion this
idiom can cause, but they're the ones I can think of.  I suspect in Twisted's
case the main reasons are the "less typing" ones rather than anything else.

-Andrew.





More information about the Twisted-Python mailing list