Python Glossary Русский

all Python 3pprintfunction

pprint.isrecursive()

checks

Checks whether a structure (list, dict, tuple) refers to itself — in which case its repr contains a placeholder instead of endless nesting.

Syntax

pprint.isrecursive(object)

print does not hang on such structures — it shows [...] — but your own recursive traversal will die with RecursionError; isrecursive tells you upfront that the data has a loop. It only sees dict, list and tuple with an unmodified __repr__: a loop through attributes of your own objects goes undetected. The same object in two places is not recursion: [x, x] gives False.

Examples

import pprint
a = [1, 2]
print(pprint.isrecursive([a, a]))  # → False
a.append(a)
print(pprint.isrecursive(a), a)  # → True [1, 2, [...]]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy