0

In Python, can any object be coerced into a string unless its __str__ method is overloaded to raise an exception?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
UnitasBrooks
  • 1,062
  • 7
  • 15

3 Answers3

1

Yes, any Python object can be converted to a str.

gmds
  • 19,325
  • 4
  • 32
  • 58
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 different str() function). The latter function is implicitly used when an object is written by the print() function.

Selcuk
  • 57,004
  • 12
  • 102
  • 110