By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

Modifying builtin type behaviour

P: n/a
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!
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
js**@cs.mcgill.ca (Jean-S?bastien Bolduc) wrote in message news:<56**************************@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
Jul 18 '05 #2

P: n/a
Jean-S?bastien Bolduc wrote:
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_.
Why?
Help would be appreciated!


I'm almost sure it isn't possible...

Gerrit.

--
Weather in Twenthe, Netherlands 17/02 15:25 UTC:
5.0°C light drizzle mostly cloudy wind 2.7 m/s WNW (57 m above NAP)
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.