Is it better to use class variables or pass parameters?

Terry Hancock hancock at anansispaceworks.com
Wed Mar 1 18:31:03 EST 2006


On 1 Mar 2006 11:32:02 -0800
"Derek Basch" <dbasch at yahoo.com> wrote:
> This one has always bugged me. Is it better to just slap a
> "self" in front of any variable that will be used by more
> than one class method or should I pass around variable
> between the methods?

My first reaction was that they should always be object
properties -- that passing values between methods of the
same object is always a bad idea. But on further reflection,
I'm not sure that's really the rule I use, so here's a
better attempt:

Are the values in question properties of the object?

If I make two calls to the object from unrelated processes,
should those values be the same, or different for each
caller?

If they need to be different for each caller, then you might
need to pass them around. If however, they are the same
because it's the same object, then they ought to be
expressed as properties of the object.

It's funny, I've never actually had any doubts about
which to use in practice, it always seems obvious, so
it's hard to recall what my actual thought process is
on the subject.

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list