Get one quick Python tip each week Weekly Python advice: one quick Python tip every week

# Python doesn't have type coercion

Share
2 min. read 3 min. video Python 3.8—3.11

Many programming languages have something called type coercion; it's where the language will implicitly convert one object to another type of object in certain circumstances.

Python does not have type coercion.

## Numeric Types and Coercion

If we add together an integer (`2`) and a floating-point number (`3.5`) in Python, we'll get back a floating-point number:

``````>>> x = 2
>>> y = 3.5
>>> x + y
5.5
``````

Python did not coerce the integer into a floating-point number; we don't have type coercion in Python. Instead, Python delegated to the integer and floating point numbers and asked those objects to add themselves together.

Whenever Python sees `x + y`, it calls the `__add__` method on `x` passing `y` to it:

``````>>> x.__add__(y)
NotImplemented
``````

In this case Python got `NotImplemented` back because integers don't know how to add themselves to floating-point numbers. This special `NotImplemented` value was returned by the`__add__` method of the integer object to let Python know that `x` (an `int`) doesn't know how to support the `+` operator with `y` (a `float`).

When Python sees this special `NotImplemented` value, it then attempts to ask `y` whether it knows how to add itself to `x`. To do this Python call the `__radd__` method on `y`, passing it `x`:

``````>>> y.__radd__(x)
5.5
``````

This adds the floating-point number to the integer from the right-hand side of the plus sign (`r` is for "right" in `__radd__`) and returns `5.5`.

So no type coercion was done here, instead, one of these types of objects knows how to operate with the other type of object when used with the plus operator.

## Python Objects Don't Support Type Coercion

A counterexample of this is strings.

What happens if we try to use the `+` operator between a string and a number in Python?

``````>>> name = "Trey"
>>> x
2
>>> name + x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
``````

Many programming languages would make the string `Trey2` above: they would concatenate that string and that number, by coercing the number into the string. In Python, we get an error instead (see [TypeError: can only concatenate str (not "int") to str][concat str] for more on that specific error).

The reason is that strings in Python don't know how to use the plus operator with numbers and numbers in Python don't know how to use the plus operator with strings, which means our code doesn't work.

So to actually accomplish what we're looking for, we need to explicitly convert the number to a string:

``````>>> name + str(x)
'Trey2'
``````

We've made a new string out of that number `2`, and we're concatenating it to our string `name` to get another string.

## Summary

Python don't have type coercion. Python doesn't ever implicitly converts one object to another type of object.

You'll always either rely on at least one of the objects you're working with knowing how to operate with the other type of object or you'll have to explicitly convert one of your objects to another type of object.

#### Series: Overlooked Fundamentals

These topics are commonly overlooked by new Python programmers.