2 ms·
You haven't work with that many languages, have you :) >>> x = 3 + "7" What type should be x now: int or str ? Also: "10" is lesser than "2" but 10 is greate
by ajuc 18y ago
You haven't work with that many languages, have you :)
>>> x = 3 + "7"
What type should be x now: int or str ?
Also: "10" is lesser than "2" but 10 is greater 2.
Should (3 + "7") > 2 return true ?
Such automatic conversions forces you to remember many arbitrary rules, and when you forget it, you have bug that shows later.
It's easier to just write that str() (or use operator %).
- stonemetal 18y agoHe converted to string before he attempted the add so it isn't a strong type vs weak type issue. It is a they haven't overloaded the + operator to perform string append. Which may or may not be logical.
- ajuc 18y agoPoster asked why he has to type print "x="+str(x). I assumed he would like to just type print "x="+x. For that code to work str.__add__(self,x) should accept int as parameter x. If that was the case, then "3" + 7 would return "37" (ok, I've thought it will be "10", my bad :)), but 7 + "3" would still cast exception. So + would be not symmetric.
- mbrubeck 18y agoOf course, str.__add__ is already asymmetric, since "3"+"7" != "7"+"3"
- stonemetal 18y agoOh well, I guess I fail at reading comprehension today. I guess that will teach me for pulling an all nighter.