getattr on objects

Ian Bicking ianb at colorstudy.com
Tue Oct 8 16:40:42 EDT 2002


On Tue, 2002-10-08 at 05:41, bromden wrote:
> I wanted to make a class which would pretend to have any
> method you can call on it (returning some default value).
> Defined a class:
>  >>> class C:
> ...     def hasattr(self, name): return 1
> ...     def getattr(self, name): return self.default
> ...     def default(self): return 'default'
[snip]

More generally, there's a bunch of "magic" methods in Python to do this
sort of thing, described here:

  http://www.python.org/doc/current/ref/specialnames.html

  Ian






More information about the Python-list mailing list