4 ms·
Ask HN: Why do I have to type print "x: "+str(x) in Python?
Why can't Python just be smart enough to cast x automatically (like every other language I work with)?
I imagine there is a not-bad reason, as Guido is pretty damn smart, but it's beyond me.
- eru 17y agoSee http://www.python.org/dev/peps/pep-0020/ http://www.python.org/dev/peps/pep-0020/: Explicit is better than implicit.
- aristus 17y ago>>> x = 1234 >>> print "x:", x x: 1234
- jibiki 17y ago>>> print "x: "+x Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects To my mind, "x: "+x is ugly. Perl style "x: $x" makes more sense. But I don't speak python, so my opinion is meaningless.
- mbrubeck 17y agoIt works if you use a comma, like the comment you replied to. Python 2.6 introduces string interpolation like Ruby/Perl. (Older versions used printf-style format strings.) print "x: {x}".format(x=1234) # New in 2.6 print "x: %d" % x # Old http://docs.python.org/tutorial/inputoutput.html#fancier-output-formatting http://docs.python.org/tutorial/inputoutput.html#fancier-out...
- eru 17y agoThis also works: >>> "X: %(x)s" % {"x":10} 'X: 10'
- iclelland 17y agoTo be fair, that's not quite the same as perl/shell-style interpolation. In perl, "$x" pulls the value of $x from the variable namespace. In your Py2.6 example, the "{x}" format in the string has nothing to do with a variable named x; rather it references the keyword args supplied to format. To simulate perl's print "x: $x"; you would print "x: {x}".format(x=x)
- vorador 17y agohttp://news.ycombinator.com/item?id=568033 http://news.ycombinator.com/item?id=568033
- vomjom 17y agoIf you want Python to "be smart enough to cast x automatically", then you want a weakly typed language. Weakly typed languages are difficult to reason about. For example, should "3" == 3? Should 3 automatically be converted to "3" in that instance, or vice-versa? Anyway, a better solution is: print "x: %s" % x
- ajuc 17y agoYou 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 17y 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 17y 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 17y agoOf course, str.__add__ is already asymmetric, since "3"+"7" != "7"+"3"
- stonemetal 17y agoOh well, I guess I fail at reading comprehension today. I guess that will teach me for pulling an all nighter.