Perhaps I am just dumb

wooks wookiz at hotmail.com
Tue Feb 12 02:07:19 EST 2002


So where do I find out what a given built in method is going to do
(source). Are these built ins overriden when you code a magic method.
Is that what happens.

It looks like I can see a practical value in this technique, vis a vis
what I want to do. There is a framework which provides an abstract
layer over a testing tool such that you can specify in a spreadsheet
what you want done without coding a script and the framework gets the
tool to do what you've asked. I want to port this framework to another
tool without changing the way the user communicates with the
framework. So if the user asks for X to be done it happens
polymorhically in whatever tool he wants. Equally if I ask for data to
be retrieved, it happens polymorphically whether the data is in CSV,
XLS or a database. In fact ADO is a good example of polymorphism in
action. Right.

hamish_lawson at yahoo.co.uk (Hamish Lawson) wrote in message news:<915a998f.0202110647.2f5a3b01 at posting.google.com>...
> 
> If an object doesn't have __add__ defined (by itself, its class, or an
> inherited class), then it won't have an interpretation *at all* for +.
> There's no 'standard behaviour' that is defaulted to. This holds true
> for the built-in types too: 4.0 + 3 has an interpretation only because
> the float type has __add__ predefined for it.
> 
> Thus
> 
>     4.0 + 3
> 
> effectively results in a call to
> 
>     (4.0).__add__(3)
> 
> The second form could actually be directly called that way in Python
> 2.2 (prior to 2.2, magic methods for built-in types weren't exposed to
> the programmer); the brackets around the 4.0 are necessary only to
> disambiguate the number from the following dot.
> 
> So there is no standard behaviour for + in the sense of some default
> action that gets performed in the absense of a defined __add__ method.
> However the __add__ method is already defined for most built-in types.
> 
> But the operator symbols provide more than just a prettier notation.
> When the operand types are different the interpreter will inspect the
> operands and will decide which operand to ask to handle the operation.
> 
> Thus 
> 
>     4 + 3.0
> 
> will effectively result in a call to
> 
>     (3.0).__radd__(4)
> 
> rather than
> 
>     (4).__add__(3.0)
> 
> since integers don't know how to add floats.
> 
> 
> Hamish Lawson



More information about the Python-list mailing list