osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Style Q: Instance variables defined outside of __init__


Irv Kalb <Irv at furrypants.com> writes:

> In this class, I get warnings on the single lines of the conceal and
> reveal methods.

This is good! It prompts the question: Why are those methods defined as
they are?

If those methods are only ever intended to set the value of an
attribute: Why not just set that attribute directly? (Remember that
Python properties allow you to postpone the decision later of whether
that attribute is a property.)

If those methods are intended to later expand to have side effects: Why
are you calling those methods from the initialiser? The initialiser
should instead just set the initial state of the attributes, avoiding
whatever side-effects are in ?conceal? etc.

So, it seems that the warning is correct. The initialiser's job is to
initialise the instance, it should not accidentally invoke a bunch of
side effects. This means the initialiser should tend to just setting
attribute values, not calling a bunch of other methods.

-- 
 \        ?It is undesirable to believe a proposition when there is no |
  `\   ground whatever for supposing it true.? ?Bertrand Russell, _The |
_o__)                                       Value of Scepticism_, 1928 |
Ben Finney