inheritance question...

Guilherme Polo ggpolo at gmail.com
Sat Jun 21 00:20:39 CEST 2008


On Fri, Jun 20, 2008 at 6:19 PM, Hamish McKenzie
<hamish at valvesoftware.com> wrote:
>
> 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
> so:
>
> 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 )

No, don't do this. Just do "avector == othervector"

>
>
> 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
>

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

>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list