Does Python really follow its philosophy of "Readability counts"?
pavlovevidence at gmail.com
Mon Jan 12 01:33:12 CET 2009
On Jan 11, 5:49 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Sun, Jan 11, 2009 at 3:15 PM, Carl Banks <pavlovevide... at gmail.com> wrote:
> > On Jan 11, 3:31 pm, Chris Rebert <c... at rebertia.com> wrote:
> >> On Sun, Jan 11, 2009 at 1:22 PM, Madhusudan.C.S <madhusuda... at gmail.com> wrote:
> >> > def somemethod(self, arg1):
> >> > self.c = 20.22
> >> > d = "some local variable"
> >> > # do something
> >> > ....
> >> > ...
> >> > def somemethod2 (self, arg2):
> >> > self.c = "Changed the variable"
> >> > # do something 2
> >> > ...
> >> > In such situations, where the Instance variables come into existence
> >> > only when they are used
> >> > it is very difficult to track the flow of code. Its obviously not
> >> > possible to remember what
> >> > instance variable was defined where, when reading some substantial
> >> > amount of code and where
> >> > it was manipulated for that matter. It becomes so very frustrating
> >> > even when reading a Class's
> >> > code with just 6-8 methods and not more than 100-150 lines of code.
> >> That's bad coding style on the part of the code writer.
> >> Conditionally-existing instance variables are *evil*.
> > Do you mean conditionally-useful instance variables evil, or that
> > conditionally-useful variables are ok but it's evil for them to
> > conditionally-exist?
> > The former I don't agree with at all.
> > If it's the latter, I believe there is something to be said for
> > variables that exist when they are needed and don't when they're not.
> > However, I acknowledge that listing all the variables you intend to
> > use in __init__ is highly comforting, even if it does belie their
> > current uselessness.
> The latter. I never even used the word "useful", so I have no idea
> where you got that from.
I was just asking for clarification of your rationale, of which I
imagined two possibilities ("conditional-usefulness is bad", or
"declaring attributes outside of __init__" is bad). However...
> To reiterate, variables which might only exist under certain
> conditions are evil, IMHO.
> This is not to say they are not useful in certain specific cases, just
> that in general there are better ways to design/structure programs to
> avoid them.
...the way you phrase this suggests to me that conditional-usefulness
*is* what you are really concerned with. Let me give you an example:
self._conditionally_existent_variable = 1
ISTM that I don't need any restructuring at all to avoid conditional
existence; all I'd have to do is add
self._conditionally_existent_variable = None
to __init__. If you think I need to restructure this code, than you
evidently care about something more than just conditional existence.
If so, what is it that's so evil about conditionally-existent
variables? (I'll leave the question open-ended this time.)
More information about the Python-list