On 04/04/13 03:36, Guido van Rossum wrote: > Consider a subclass of int() that overrides __repr__() and __str__() to > print something fancy (maybe it defaults to hex; maybe it's an enum :-). I > want to be able to say repr(int(x)) and get the standard decimal > representation. Same with strings. If int() or str() were allowed to return > a subclass instance, this wouldn't work, and I'd have to resort to > draconian measures. int and str currently are allowed to return subclass instances. > (Betcha the first few solutions you come up with don't > even work. :-) Well, I'm always game to learn something. Challenge accepted. # Force a string subclass s to a built-in string. ''.join(s) # Force an int subclass n to a built-in int. (0).__add__(n) # And similarly for float subclass. 0.0.__add__(x) -- Steven
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