473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Modifying builtin type behaviour

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
2 1629
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB...
2
by: Craig Zoretich | last post by:
Hi, Is it possible to change how the "def" builtin command works? Specifically I want to modify def to write to a log file when a function executes (for instance the name of the function, when...
5
by: Blair Hall | last post by:
Can anyone please tell me how to correctly use a built in function when there is a function of the same name in local scope? Here is an example. Suppose the following is in myApply.py: def...
6
by: qwweeeit | last post by:
Hi all, when I was young I programmed in an interpreted language that allowed to modify itself. Also Python can (writing and running a module, in-line): fNew =open("newModule.py",'w') lNew=...
11
by: Shaun | last post by:
Hi, I'm trying to overload the divide operator in python for basic arithmetic. eg. 10/2 ... no classes involved. I am attempting to redefine operator.__div__ as follows: # my divide...
7
by: Jan Gregor | last post by:
Hello folks I want to apply changes in my source code without stopping jython and JVM. Preferable are modifications directly to instances of classes. My application is a desktop app using swing...
7
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
0
by: Rhamphoryncus | last post by:
I know similar things have been argued before, but little things (like the joined name implying a copy) can make a big difference. That and I'm providing a simple implementation that works right...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.