Python Glossary Русский

Python 3.4+abcfunction

abc.get_cache_token

abc utilities

Return the current ABC cache token; changes when virtual subclasses are registered.

Syntax

abc.get_cache_token()

It exists because ABCs accept virtual subclasses at runtime via register(), so the answer isinstance() gives for the same pair can change later. If you cache such checks yourself, store the token alongside and drop the cache when it differs; the token is an opaque value meaningful only for equality comparison, not for ordering or arithmetic.

Examples

import abc
print(isinstance(abc.get_cache_token(), int))   # → True
t = abc.get_cache_token()
class Virtual(abc.ABC): pass
print(Virtual.register(list) is list)   # → True
print(abc.get_cache_token() != t)   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy