inheritance question...

Hamish McKenzie hamish at
Fri Jun 20 23:19:37 CEST 2008

I have this class:

class Vector(object):
     TOL = 1e-5
     def __eq__( self, other, tolerance=TOL ):
           print tolerance

shortened for clarity obviously.  so I want to subclass this class like

class BigVector(Vector)
     TOL = 100

for example if I was working with large vectors which I knew would never
be very close hence the large tolerance.  this doesn't work however -
the TOL class variable, while overridden in BigVector, is still using
the Vector.TOL variable in the __eq__ method.

which kinda makes sense to a certain degree, but how do I get the
behaviour where doing:

BigVector().__eq__( otherVec )

prints 100 instead of 1e-5?

does this question make sense?  not sure how clearly I'm phrasing my
question...  any of you guys python experts?

I *could* do this, but its ugly:

class Vector(object):
     TOL = 1e-5
     def __eq__( self, other, tolerance=None ):
           if tolerance is None: tolerance = self.TOL
           print tolerance

More information about the Python-list mailing list