Python Glossary Русский

all Python 3Built-in functionsterm

object

OOP

The base class of every Python class. Provides the default __str__, __repr__, __eq__, __hash__ and other magic methods.

Syntax

object()

A bare object() is not a general-purpose bag of fields: it has no __dict__, so o.foo = 1 fails with AttributeError; it is, however, the perfect unique sentinel when None is a legitimate value. The inherited __eq__ and __hash__ compare identity, so two semantically equal instances are unequal until you define __eq__ — and the moment you do, the class stops being hashable unless you define __hash__ as well.

Examples

o = object()
print(type(o))  # → <class 'object'>
print(isinstance(42, object))  # → True
print(isinstance('hi', object))  # → True
class A(object): pass  # явно наследуется
print(A.__bases__)  # → (<class 'object'>,)
print(dir(object())[:2])  # → ['__class__', '__delattr__']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy