Modifying builtin type behaviour

Michele Simionato michele.simionato at poste.it
Fri Feb 13 13:04:50 EST 2004


jseb at cs.mcgill.ca (Jean-S?bastien Bolduc) wrote in message news:<56e1eff0.0402121015.2de0d427 at posting.google.com>...
> I'm afraid this is a silly question, to which I know the answer
> already. But let me ask anyway...
> 
> In Python, is there a way to modify a builtin type's methods? for
> instance, modifying int.__add__ to behave as multiplication, I would
> obtain 2+5 == 10. I emphasize that I don't want to modify the Python
> source, but change the types behaviour _from within Python_.
> 
> Help would be appreciated!

There is no way of doing what you asked in Python, since Guido thinks 
it is a Bad Idea (TM). He is probably right; on the other hand, Matz,
the creator of Ruby, thinks it is a good idea, so you may want to check
Ruby for this kind of games. In Python you need to subclass int to
modify its methods, which is probably not what you want.

   Michele Simionato



More information about the Python-list mailing list