Class Variable Question

Alan Gauld alan.gauld at bt.com
Tue Apr 10 09:38:18 EDT 2001


Robert Johnson wrote:
> > The original poster was very explicitly talking about a *class* 
> Actually I was referring to the instance of the class 
> (I use the term "Object" as meaning the instance of the class).  

But you in fact used ClassObject, hence it was ambiguous at best.

> I think the feature I was looking for was something similar 
> to the Options Explicit option of Visual Basic....  
> I can't imagine it not being a source of bugs, 

It is a source of bugs, even on quite small preograms.
Whether it is a major source is another matter, I've 
been bitten by it maybe 3 or 4 times in the last 18 months...

In one case it was very hard to find since I was using 
an indirect reference to the class - iterating over a 
heterogenous list of objects and so wasn't sure which 
class had the error in. But normally its not a major issue.

Some kind of optional warning mode that alerted me to 
the creation of attributes outside a class definition 
would IMHO be useful. Alternatively a code checker 
(pyLint anyone?) that could flag such issues. 

Provided you use good OO practice such as accessor methods
and avoid the from M import * style in your programs this 
is unlikely to be a major issue since you won't be 
accessing module variables etc directly - the name 
of the class/object in front gives a strong pointer 
of where to look for the problem.

> I am coming from the C++ world where if one wanted to 
> extend a class, one would override the base class 
> (or insert into the original class if you had the source) 

Hmm, the latter is a dangerous practice if there's any 
chance of the class being used by anyone else (reuse being 
one of the Goals of OO!).

> seemed odd to me that one could just add things on the fly, 
> but that is just my bias.

It is odd in traditional OOP terms but it is consistent 
with the "market" that Python aims for - rapid development 
with maximum flexibility. In that sense it's like C and 
just like C, the programmer does need to excercise a little 
extra care at times. Python always does what you ask it 
to, its just that what we ask isn't always what we mean!

Alan g



More information about the Python-list mailing list