Restricting methods in derived classes

Mark McEahern marklists at mceahern.com
Wed Sep 11 18:29:37 EDT 2002


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

// m

-





More information about the Python-list mailing list