New Python 3.0 string formatting - really necessary?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Dec 21 11:09:04 EST 2008


On Sun, 21 Dec 2008 15:30:34 +0000, Duncan Booth wrote:

> Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
> 
>>> a+b+c+d might execute a.__add__(b,c,d) allowing more efficient string
>>> concatenations or matrix operations, and a%b%c%d might execute as
>>> a.__mod__(b,c,d).
>> 
>> But that needs special casing strings and ``%`` in the comiler, because
>> it might not be always safe to do this on arbitrary objects.  Only in
>> cases where the type of `a` is known at compile time and ``a % b``
>> returns an object of ``type(a)``.
>> 
> I could be wrong, but I don't see that would be the case.
> 
> I think it would be safe (in this hypothetical universe) any time that
> 'a' had a method __mod__ which accepted more than one argument.

And returns an object of ``type(a)`` or at least a "duck type" so that it 
is guaranteed that ``a.__mod__(b, c)`` really has the same semantics as 
``a.__mod__(b).__mod__(c)``.  For arbitrary objects `a`, `b`, and `c` 
that are not known at compile time, how could the compiler decide if it 
is safe to emit code that calls `a.__mod__()` with multiple arguments?

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list