TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Why do I have to type print "x: "+str(x) in Python?

6 pointsby noaharcabout 16 years ago
Why can't Python just be smart enough to cast x automatically (like every other language I work with)?<p>I imagine there is a not-bad reason, as Guido is pretty damn smart, but it's beyond me.

5 comments

aristusabout 16 years ago
<p><pre><code> &#62;&#62;&#62; x = 1234 &#62;&#62;&#62; print "x:", x x: 1234</code></pre>
评论 #571046 未加载
ajucabout 16 years ago
You haven't work with that many languages, have you :)<p>&#62;&#62;&#62; x = 3 + "7"<p>What type should be x now: int or str ?<p>Also: "10" is lesser than "2" but 10 is greater 2.<p>Should (3 + "7") &#62; 2 return true ?<p>Such automatic conversions forces you to remember many arbitrary rules, and when you forget it, you have bug that shows later.<p>It's easier to just write that str() (or use operator %).
评论 #571153 未加载
vomjomabout 16 years ago
If 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.<p>For example, should "3" == 3? Should 3 automatically be converted to "3" in that instance, or vice-versa?<p>Anyway, a better solution is: print "x: %s" % x
eruabout 16 years ago
See <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">http://www.python.org/dev/peps/pep-0020/</a>:<p><pre><code> Explicit is better than implicit.</code></pre>
voradorabout 16 years ago
<a href="http://news.ycombinator.com/item?id=568033" rel="nofollow">http://news.ycombinator.com/item?id=568033</a>