Is there a way to 'mask out' inherited methods?

Ralf Juengling juenglin at informatik.uni-freiburg.de
Thu May 2 16:50:28 EDT 2002


Hi,

I'm fiddling around with some new 2.2.-features. I want to
inherit from a builtin-type but want to use only a subset
of its functionality:


class Vector(list):
    """A class to represent vectors.
    """

    def __init__(self, elems):
        supself = super(Vector, self)
        supself.__init__([float(e) for e in elems])

    def __chklen(self, other):
        if len(self) != len(other):
            raise ValueError, "Vectors must have the same length."
        
    def __add__(self, other):
        self.__chklen(other)
        return Vector([x+y for x, y in zip(self, other)])

    def dot(self, other):
        """Dot product of two vectors.
        """
        self.__chklen(other)
        _dot = 0
        for s in [x*y for x, y in zip(self, other)]:
            _dot += s
        return _dot
        


I.e. Vector should keep or inherit methods meaningful to vectors
(e.g. '__str__', '__getitem__', '__cmp__' and so on), and should
somehow undefine or mask out any methods not meaningful to it
('append', 'reverse', ..).

Any idea how to achieve that easily?

Ralf



More information about the Python-list mailing list