why no automatic conversion in string concatenation?
J. Clifford Dyer
jcd at sdf.lonestar.org
Tue Nov 13 12:44:12 EST 2007
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 "<stdin>", line 1, in <module>
> 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
More information about the Python-list
mailing list