What does ! mean in Python?

Viewed 38

I am trying to build a list containing children lisats inside the original list, and iterate over that list. I have a book called Python Cookbook, they used the code below to solve the question. What is !r?

class Node():
  def __init__(self, value):
    self._value = value
    self._children = []
  def __repr__():
    return 'Node({!r})'.format(self._value)
  def add_child(self, node):
    self._children.append(node)
  def __iters__(self):
    return iter(self._children)
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
for ch in root:
  print(ch)

1 Answers

That is part of string formatting. In this context, it is a conversion flag. It specifies the use of the repr of the object.

Related