Modifying builtin type behaviour

  • Thread starter Jean-S?bastien Bolduc
  • Start date
J

Jean-S?bastien Bolduc

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!
 
M

Michele Simionato

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
 
G

Gerrit

Jean-S?bastien Bolduc said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top