Issue with new-style classes and operators

Jan D. Wegner Jan.Wegner at zuken.de
Mon Nov 25 07:27:06 EST 2002


Jan Decaluwe schrieb:
> Hi:
> 
> I'm confused by the following behavior of new-style classes
> and operators:
> 
>     class MyInt(object):
		
try:  classMyInt(int):

or MyInt isn't considered int(eger)

>         def __init__(self, val):
>             self.val = val        
>         def __getattr__(self, attr):
>             return getattr(self.val, attr)
> 
> 
> 
>>python
> 
> Python 2.2.2 (#1, Oct 16 2002, 19:59:11) 
> [GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>from MyInt import MyInt
>>>>a = MyInt(3)
>>>>a.__add__(4)
>>>
> 7
> 
>>>>a + 4
>>>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: unsupported operand types for +: 'MyInt' and 'int'
> 
> 
> 
> I don't think is can be the intention. Or can it?
> 
> Regards, Jan
> 
> --
> Jan Decaluwe
> Losbergenlaan 16
> B-3010 Leuven
> mailto:jan at jandecaluwe.com


-- 
Jan Wegner          Software Engineer         Zuken GmbH
Vattmannstrasse 3                      D-33100 Paderborn
     Tel: +49 5251 150627      Fax: +49 5251 150700
----*... and may all your GUIs be fast and friendly.*----




More information about the Python-list mailing list