Restricting methods in derived classes

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Thu Sep 12 18:01:37 EDT 2002


Mark McEahern <marklists at mceahern.com> wrote:
>[Huaiyu Zhu]
>> 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?
>
>Would something like this work?
>
>$ python
>Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
>[GCC 2.95.3-5 (cygwin special)] on cygwin
>Type "help", "copyright", "credits" or "license" for more information.
>>>> class foo(dict):
>...     def not_defined(self, *args, **kwargs):
>...             raise NotImplementedError
>...     update = not_defined
>...
>>>> f = foo()
>>>> f['a'] = 1
>>>> f
>{'a': 1}
>>>> d = {'a': 2}
>>>> f.update(d)
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "<stdin>", line 3, in not_defined
>NotImplementedError

I was hoping for a way to confirm wanted methods, because there are more
methods that I don't want.  But the above is good enough for preventing
obvious mistakes.  Thank you.

Huaiyu



More information about the Python-list mailing list