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.
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
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.
NotImplemented value was returned by the
__add__ method of the integer object to let Python know that
int) doesn't know how to support the
+ operator with
When Python sees this special
NotImplemented value, it then attempts to ask
y whether it knows how to add itself to
To do this Python call the
__radd__ method on
y, passing it
>>> 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
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.
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.
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.
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.
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.
Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.