0

Python is a very easy to use language, but I often run into the issue where I have

x = "hello world = " + 42

Why can't python have a built in function to auto-convert 42 to a string to avoid this:

x = "hello world = " + str(42)

On so many levels python is short to write code, but here we developers have to spend extra effort.

Is there any technical reason on low level python interpreter side for this design rule of the python language giving a concrete reason why it is like this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    Because [Explicit is better than implicit.](https://www.python.org/dev/peps/pep-0020/) It's a dupe, too: https://stackoverflow.com/q/43956430/3001761. – jonrsharpe Jun 04 '20 at 18:28
  • 2
    Well, from Python 3.6 you can actually do `x = f"hello world = {42}"` which is both short and simple, and explicit... Of course I'm assuming you ask for the general case where `42` will be replaced with a variable – Tomerikoo Jun 04 '20 at 18:32
  • 1
    There's no technical reason, it is a design decision. These sorts of implicit conversions go against the philosophy of the language. Python is generally considered *strongly typed*, so every object has a type and knows its type, and the runtime doesn't do implicit type coercions to try to *guess* at what you mean't by operations on mismatched types. Instead, it errors, which is a *good thing* in my opinion. If you want this behavior, you can try javascript, but personally, I hate this sort of thing because creates all sorts of bugs – juanpa.arrivillaga Jun 04 '20 at 18:33
  • Does this answer your question? [Reason for the inability to concatenate strings and ints in Python](https://stackoverflow.com/questions/43956430/reason-for-the-inability-to-concatenate-strings-and-ints-in-python) – Tomerikoo Jun 04 '20 at 18:39

2 Answers2

2

In short, because Python is strongly typed, meaning it avoids implicit type conversions as much as possible. That's part of the philosophy of the language: "Explicit is better than implicit" and "In the face of ambiguity, refuse the temptation to guess."

As an advantage, this avoids ambiguous expressions like "1" + 1 and 1 + "1". Should the result be 2 or "11"? Should it depend on which one comes first or should there be a type "hierarchy"? JavaScript on the other hand evaluates both as "11".


In this case, if you're going to be printing x, use print instead since it can convert objects to a string implicitly.

print("hello world =", 42)

Otherwise you can use string interpolation:

meaning = 42
x = f"hello world = {meaning}"

or the older equivalent:

x = "hello world = {}".format(meaning)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0
x = 'hello world 42' # if 42 is fixed, which is a bit odd

# if 42 is stored in a variable:
number = 42
x = f'hello world = {number}'

Regarding the WHY, simple version. Let's reverse the statement.

x = 42 + 'foo'

Should python now somehow transform the 'foo' into a number to make addition work?

Now the long version. Operation + is defined by __add__ magic method of a class. So when you call 's' + 's1' you're actually calling:

's'.__add__('s1')

So there's an operator overloading for strings that says that addition is actually concatenation. But it doesn't do implicit str() conversion, because that would have been misleading (see simple example).

However you can make your own class for strings that would do that.

Python does implicit type conversion where it makes sense, for example when adding int and float it will convert both to float.