In Python, can any object be coerced into a string unless its __str__
method is overloaded to raise an exception?
Asked
Active
Viewed 225 times
0

Selcuk
- 57,004
- 12
- 102
- 110

UnitasBrooks
- 1,062
- 7
- 15
3 Answers
1
Everything in Python is an object.
The type object has a method for string coercion as you’ve noted. If some object did not have this method defined, it would not be an object. This contradicts the first statement. QED.

munk
- 12,340
- 8
- 51
- 71
-1
Yes:
https://docs.python.org/3/library/stdtypes.html
Some operations are supported by several object types; in particular, practically all objects can be compared, tested for truth value, and converted to a string (with the
repr()
function or the slightly differentstr()
function). The latter function is implicitly used when an object is written by theprint()
function.

Selcuk
- 57,004
- 12
- 102
- 110