Type emulation issues with new style classes

Chris feb04.20.netman at spamgourmet.com
Sat Feb 28 06:33:54 EST 2004


Is there any way to make the class Z behave the same way as class Y?

Chris

class Y:
    value = 42
    def __hasattr__(self, name):
        if name == '__int__':
            return True
    def __getattr__(self, name):
        if name == '__int__':
            return lambda: self.value

class Z(object):
    value = 42
    def __hasattr__(self, name):
        if name == '__int__':
            return True
    def __getattr__(self, name):
        if name == '__int__':
            return lambda: self.value

>>> y, z = Y(), Z()
>>> print int(y)
42
>>> print int(z)
TypeError: int() argument must be a string or a number





More information about the Python-list mailing list