Type emulation issues with new style classes

Chris feb04.20.netman at spamgourmet.com
Sat Feb 28 17:44:21 CET 2004


I need to have Python call the the emulation methods for numbers, lists,
etc, when those methods are provided though __getattr__ rather than defined
in the instance or class __dict__.  I've been up and down PEP 252 trying to
determine how Python determines the presence or absence of emulation methods
for new style classes, but I'm not having any luck.

"Carmine Noviello" wrote:
> class Z(object):
>     value = 42
>     def __hasattr__(self, name):
>         if name == '__int__':
>             return True
>     def __int__(self):
>         return self.value
>
>
> Umm, maybe I haven't understood what you need.

I need to know how I can do what I previously demonstrated with an old style
class, that is hook into Python's introspection mechanism to dynamically
provide an  __int__ method (or optionally __str__, __lt__, __le__, __eq__
,__ne__, __gt__, __ge__, __len__, __getitem__, __add__, __sub__, __mul__,
__floordiv__, __mod__, __divmod__, __pow__, __lshift__, ... ad nausuem).
Old style classes will let me do this, but I cannot determine how to do this
with new style classes.

Chris





More information about the Python-list mailing list