The following small program is giving me some unexpected results with Python 2.2.1: class Int(object): def __int__(self): return 10 class Float(object): def __float__(self): return 10.0 class Long(object): def __long__(self): return 10L class Complex(object): def __complex__(self): return (10+0j) def attempt(f,arg): try: return f(arg) except Exception,e: return str(e.__class__.__name__)+': '+str(e) for f in int,float,long,complex: for t in Int,Float,Long,Complex: print f.__name__ + '(' + t.__name__ + ')\t\t', print attempt(f,t()) ----- results ------ int(Int) 10 int(Float) TypeError: object can't be converted to int int(Long) TypeError: object can't be converted to int int(Complex) TypeError: object can't be converted to int *** OK, int() seems to work as expected float(Int) TypeError: float() needs a string argument float(Float) 10.0 float(Long) TypeError: float() needs a string argument float(Complex) TypeError: float() needs a string argument *** float() seems to work, but what's with the error message about strings? long(Int) TypeError: object can't be converted to long long(Float) TypeError: object can't be converted to long long(Long) 10 long(Complex) TypeError: object can't be converted to long **** OK, long seems to work as expected complex(Int) TypeError: complex() arg can't be converted to complex complex(Float) (10+0j) complex(Long) TypeError: complex() arg can't be converted to complex complex(Complex) TypeError: complex() arg can't be converted to complex **** I can understand complex() handling Float implicitly, but only if it also handles Complex! And if it does handle Float implicitly, shouldn't all of these handle everything? Comments? -Dave +---------------------------------------------------------------+ David Abrahams C++ Booster (http://www.boost.org) O__ == Pythonista (http://www.python.org) c/ /'_ == resume: http://users.rcn.com/abrahams/resume.html (*) \(*) == email: david.abrahams@rcn.com +---------------------------------------------------------------+
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4