Restricting methods in derived classes

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Sep 11 17:43:51 EDT 2002


This should be something that the new style class solves but I can't figure
out exactly how:

I need a class that is similar to dict, but, besides providing several
additional methods, it only inherits a subset of dict methods.  What is the
best way to say that only a subset of methods are inherited?

Using the UserDict approach requires wrappers for all the inherited methods,
such as
    def keys(self):     return self.dict.keys()
    def items(self):    return self.dict.items()
This has a function call overhead.  I'd prefer something like
    items = self.dict.items
but of course self is not defined here.

Using the "class MyClass(dict)" approach, how do I say that methods such as
update, __setitem__ are not inherited?  I know the set/get attr trick, but
that adds a function call overhead on every method.

What I'm looking for is a way to say which methods from dict are inherited
in MyClass at the time the class is defined, without incuring a cost when
the methods are called.  Is this possible?

Huaiyu



More information about the Python-list mailing list