Repaso
Español
PythonIntermediatePY-12

print([obj]) where obj defines both __str__ and __repr__. Which one is used for obj?

Options

  1. __str__
  2. __repr__ (Correct option)
  3. __format__
  4. Neither: it prints the memory address

Answer

__repr__

Why

__str__ returns friendly text for users. __repr__ returns precise text for developers.

When Python prints a list, it shows each element with repr(), because it wants unambiguous output.

So print([obj]) uses __repr__, while print(obj) uses __str__.

Remember

Containers show their items with repr().

Key terms

Practice this question in Repaso