Overriding methods per-object

George Sakkis george.sakkis at gmail.com
Sat Apr 18 01:53:28 EDT 2009


On Apr 17, 9: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?


FYI this works as you expect if GeneralTypeOfObject is an old-style
class (i.e. does not inherit from object). If this feature is so more
important than all those that come with new-style classes, you have
control over the involved classes and don't care about Python 3.x
(where old-style classes are gone), you may choose to downgrade
GeneralTypeOfObject to old-style.

George



More information about the Python-list mailing list