why no automatic conversion in string concatenation?

Michael Pelz Sherman mpelzsherman at yahoo.com
Tue Nov 13 20:09:32 CET 2007


Thanks Cliff. Not to belabor this point - clearly it's just something I'll have to get used to - but isn't the choice of the "+" as the concatenation operator part of the problem here? A more "explicit" choice would have been an "append()" function, would it not? Or at least a non-ambiguous character.

Obviously python *does* know that I'm concatenating, not adding, due to the type of the LH object, and it would save me a lot of time if I didn't have to explicitly convert my objects to strings.

Any rule can be abused...

- Michael

"J. Clifford Dyer" <jcd at sdf.lonestar.org> wrote: On Tue, Nov 13, 2007 at 07:15:06AM -0800, Michael Pelz Sherman wrote regarding why no automatic conversion in string concatenation?:
> 
>    As a Java & PHP developer, I find it kind of annoying that I have to
>    explicitly convert non-string variables to strings when concatenating
>    them, especially when python is quite capable of doing the conversion
>    automatically.
>    i.e.:
>    >>> myBool = True
>    >>> print myBool
>    True
>    >>> print "myBool is " + myBool
>    Traceback (most recent call last):
>      File "", line 1, in 
>    TypeError: cannot concatenate 'str' and 'bool' objects
>    >>> print "myBool is " + str(myBool)
>    myBool is True
>    Can anyone explain why this is so? 

Because python doesn't know if '1' + 1 should equal 2 or '11' and would rather you mad that decision.  Should it be different than 1 + '1'?  

or to put it more succinctly, because "explicit is better than implicit."
In fact, I think it's more often the case that I have string data that I need to treat as integers than the other way around (input from stdin and textfiles for example).

> Are there any plans to change this
>    in python 3000?

I hope not, and I don't think so.

>    Thanks,
>    - Michael

No problem. 

Cheers,
Cliff


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071113/1609e48e/attachment.html>


More information about the Python-list mailing list