Overriding methods per-object

Aaron Brady castironpi at gmail.com
Fri Apr 17 21:37:52 EDT 2009


On Apr 17, 8:22 pm, Pavel Panchekha <PavPanche... at gmail.com> wrote:
> I've got an object which has a method, __nonzero__
> The problem is, that method is attached to that object not that class
>
> > a = GeneralTypeOfObject()
> > a.__nonzero__ = lambda: False
> > a.__nonzero__()
>
> False
>
> But:
>
> > bool(a)
>
> True
>
> What to do?

The docs don't say you can do that:

Special method names
A class can implement certain operations that are invoked by special
syntax (such as arithmetic operations or subscripting and slicing) by
defining methods with special names.

Should you be able to?



More information about the Python-list mailing list