Why is there no automatic conversion of numbers to strings in Python?

Source: Internet
Author: User
Print "I am" + ten + "years old" would raise error. But it's OK in many other languages. Java,eg.

Reply content:

Because Python prohibits cross-type (+), it only
    1. (+): int * Int→int
    2. (+): String * string→string
And no
    • (+): int * Str→⊤
Both JavaScript and PHP allow (+) to do cross-type calculations, and JavaScript uses num * str→str,php to use num * str→num. The Zen of Python has such a sentence.
Explicit is better than implicit.

Common mistakes of Python programmers It also explains the sentence.
Type conversions exist only in numeric types
In Python, an expression such as 123+3.145 can work-it automatically converts the integer type to float, and then uses floating-point arithmetic. But the following code will go wrong:
S = "42"
I = 1
X = S + I # type Error
This is also intentional, because it is unclear: whether to convert a string to a number (to add), or to convert a number to a string (join)? In Python, we think "explicit is better than ambiguity" (i.e., EIBTI (Explicit is better than implicit), so you have to manually convert the type:
X = Int (S) + I # do addition: 43
X = S + str (I) # string join: "421"

For a strongly typed language like Python, it is normal to have no automatic conversion of numbers to strings.

Like other can implement the number to the string automatic conversion of the language is how scary, give you a look at the picture (figure is everyone on the test instructions, not fully meet the, but also can refer to)

I want to answer, many people are very curious, many languages are particularly useful in the implicit conversion:

   
    echo 'Result:'.(1+2+3);?>
Python is a strongly typed language, and strong types naturally have strong types of benefits.

I don't know much about PHP and JavaScript, but I've written a lot of exploit codes with their string and number-conversion features.

Wait for someone familiar with them to tell you how much this automatic conversion is. Python is strongly typed, so it's natural to not support this feature, and if you're using a weak-type language like Perl, you'll be supported.

For other reasons, there's a lot of talk upstairs, and the better way to do this is to use a formatted string or a string template. Static strong type efficiency is high.
However, JS does not distinguish between the number + and the string + automatic conversion, you can play the custom new type of object's black Magic:
({valueOf:function(){return1;}}+{valueOf:function(){return1;}})//猜猜等于几?
The topic itself involves only the strength type and the string connection, but there are answers that extend the problem to print/echo and operators.

First, the strength type.
How I feel:
Strongly typed languages consider different types of values to be different, and weak type languages think that different types but related values are the same in most cases.
So in Python,
1!='1'
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.