chunk_id large_stringlengths 36 36 | doc_type large_stringclasses 2
values | category large_stringclasses 17
values | document large_stringclasses 50
values | version_raw large_stringclasses 590
values | chunk_index int64 0 262 | text large_stringlengths 11 5.69k | file_path large_stringclasses 882
values |
|---|---|---|---|---|---|---|---|
3fbe8cfa-bc98-50b7-a81e-c657cdba9c58 | docs | python | python_datamodel | 311 | 61 | [`__iter__()`](#object.__iter__ "object.__iter__") to `None`, the class is not iterable, so calling
[`iter()`](../library/functions.html#iter "iter") on its instances will raise a [`TypeError`](../library/exceptions.html#TypeError "TypeError") (without
falling back to [`__getitem__()`](#object.__getitem__ "object.__get... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
e76a7e88-eddf-5b9d-a307-797e60a5bbaa | docs | python | python_datamodel | 311 | 62 | method (special-cased so you need not declare it as such) that takes the class
of which an instance was requested as its first argument. The remaining
arguments are those passed to the object constructor expression (the call to the
class). The return value of [`__new__()`](#object.__new__ "object.__new_... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
f93f5308-29ca-55f3-9ec7-d771668b46ad | docs | python | python_datamodel | 311 | 63 | and the remaining arguments are the same as were passed to the object constructor.
If [`__new__()`](#object.__new__ "object.__new__") does not return an instance of *cls*, then the new instance’s
[`__init__()`](#object.__init__ "object.__init__") method will not be invoked.
[`__new__()`](#object.__new... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
365b983a-a1df-5006-a386-57cd8bc7225e | docs | python | python_datamodel | 311 | 64 | method, the derived class’s [`__init__()`](#object.__init__ "object.__init__") method, if any, must explicitly
call it to ensure proper initialization of the base class part of the
instance; for example: `super().__init__([args...])`.
Because [`__new__()`](#object.__new__ "object.__new__") and [`__init... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
9c40d11c-dfa5-5402-873d-a1a0eac59603 | docs | python | python_datamodel | 311 | 65 | [`__del__()`](#object.__del__ "object.__del__") method, the derived class’s [`__del__()`](#object.__del__ "object.__del__") method,
if any, must explicitly call it to ensure proper deletion of the base
class part of the instance.
It is possible (though not recommended!) for the [`__del__()`](#object.__... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
1166c24f-e4ec-5069-874a-ec190c695fd8 | docs | python | python_datamodel | 311 | 66 | the reference count for `x` by one, and the latter is only called when
`x`’s reference count reaches zero.
**CPython implementation detail:** It is possible for a reference cycle to prevent the reference count
of an object from going to zero. In this case, the cycle will be
later detected and delet... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
61c878bb-beb7-5428-a917-b19dd87dc14d | docs | python | python_datamodel | 311 | 67 | is printed to `sys.stderr` instead. In particular:
* [`__del__()`](#object.__del__ "object.__del__") can be invoked when arbitrary code is being executed,
including from any arbitrary thread. If [`__del__()`](#object.__del__ "object.__del__") needs to take
a lock or invoke any other blocking resour... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
7041d7ba-9b0d-5184-b8fb-f3a51d6224ff | docs | python | python_datamodel | 311 | 68 | [`__del__()`](#object.__del__ "object.__del__") method is called.
object.\_\_repr\_\_(*self*)[¶](#object.__repr__ "Link to this definition")
: Called by the [`repr()`](../library/functions.html#repr "repr") built-in function to compute the “official” string
representation of an object. If at all possible, ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
ffea1d0e-7fb5-5484-83ee-cef70ff77adc | docs | python | python_datamodel | 311 | 69 | is information-rich and unambiguous.
object.\_\_str\_\_(*self*)[¶](#object.__str__ "Link to this definition")
: Called by [`str(object)`](../library/stdtypes.html#str "str") and the built-in functions
[`format()`](../library/functions.html#format "format") and [`print()`](../library/functions.html#print "pri... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
ba8bee47-da8d-5935-9e5f-3ae623ddcfc9 | docs | python | python_datamodel | 311 | 70 | object.\_\_bytes\_\_(*self*)[¶](#object.__bytes__ "Link to this definition")
: Called by [bytes](../library/functions.html#func-bytes) to compute a byte-string representation
of an object. This should return a [`bytes`](../library/stdtypes.html#bytes "bytes") object.
object.\_\_format\_\_(*self*, *format\_spec*)... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
12f49dee-64fd-5e20-93d3-cb283e408588 | docs | python | python_datamodel | 311 | 71 | delegate formatting to one of the built-in types, or use a similar
formatting option syntax.
See [Format Specification Mini-Language](../library/string.html#formatspec) for a description of the standard formatting syntax.
The return value must be a string object.
Changed in version 3.4: The \_\_f... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
81319006-a736-5b2e-a8b1-4b265595eaf6 | docs | python | python_datamodel | 311 | 72 | object.\_\_ge\_\_(*self*, *other*)[¶](#object.__ge__ "Link to this definition")
: These are the so-called “rich comparison” methods. The correspondence between
operator symbols and method names is as follows: `x<y` calls `x.__lt__(y)`,
`x<=y` calls `x.__le__(y)`, `x==y` calls `x.__eq__(y)`, `x!=y` calls
`... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
cd249f21-eb40-5912-85bc-51d0cd2f0d41 | docs | python | python_datamodel | 311 | 73 | By default, `object` implements [`__eq__()`](#object.__eq__ "object.__eq__") by using `is`, returning
[`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented") in the case of a false comparison:
`True if x is y else NotImplemented`. For [`__ne__()`](#object.__ne__ "object.__ne__"), by de... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
6ae3edfe-a059-577d-8113-d364eb9a8266 | docs | python | python_datamodel | 311 | 74 | custom comparison operations and are usable as dictionary keys.
There are no swapped-argument versions of these methods (to be used when the
left argument does not support the operation but the right argument does);
rather, [`__lt__()`](#object.__lt__ "object.__lt__") and [`__gt__()`](#object.__gt__ "o... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
b1224306-a2be-549b-8ee2-12ae860af161 | docs | python | python_datamodel | 311 | 75 | When no appropriate method returns any value other than [`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented"), the
`==` and `!=` operators will fall back to `is` and `is not`, respectively.
object.\_\_hash\_\_(*self*)[¶](#object.__hash__ "Link to this definition")
: Called by built-in... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
5ef0ddbf-ab66-5b8e-acf9-31511cc8cc26 | docs | python | python_datamodel | 311 | 76 | return hash((self.name, self.nick, self.color))
```
Note
[`hash()`](../library/functions.html#hash "hash") truncates the value returned from an object’s custom
[`__hash__()`](#object.__hash__ "object.__hash__") method to the size of a [`Py_ssize_t`](../c-api/intro.html#c.Py_ssize_t "Py_ssize_t... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
55e33da3-af31-5320-bf2c-1ca4c43e6ab7 | docs | python | python_datamodel | 311 | 77 | [`__hash__()`](#object.__hash__ "object.__hash__"), its instances will not be usable as items in hashable
collections. If a class defines mutable objects and implements an
[`__eq__()`](#object.__eq__ "object.__eq__") method, it should not implement [`__hash__()`](#object.__hash__ "object.__hash__"), since t... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
8a7c8c58-79ad-5b4a-a93d-fa87717f27ca | docs | python | python_datamodel | 311 | 78 | will have its [`__hash__()`](#object.__hash__ "object.__hash__") implicitly set to `None`. When the
[`__hash__()`](#object.__hash__ "object.__hash__") method of a class is `None`, instances of the class will
raise an appropriate [`TypeError`](../library/exceptions.html#TypeError "TypeError") when a program ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
466355d8-130b-5c91-bd00-b1b4b3d68175 | docs | python | python_datamodel | 311 | 79 | A class which defines its own [`__hash__()`](#object.__hash__ "object.__hash__") that explicitly raises
a [`TypeError`](../library/exceptions.html#TypeError "TypeError") would be incorrectly identified as hashable by
an `isinstance(obj, collections.abc.Hashable)` call.
Note
By default, the [`__has... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
0738092f-5314-5946-8e47-13d7177966d6 | docs | python | python_datamodel | 311 | 80 | (and it typically varies between 32-bit and 64-bit builds).
See also [`PYTHONHASHSEED`](../using/cmdline.html#envvar-PYTHONHASHSEED).
Changed in version 3.3: Hash randomization is enabled by default.
object.\_\_bool\_\_(*self*)[¶](#object.__bool__ "Link to this definition")
: Called to implement truth ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
04dad8f7-dad9-5597-8afc-d8ee8a6c8cf4 | docs | python | python_datamodel | 311 | 81 | object.\_\_getattr\_\_(*self*, *name*)[¶](#object.__getattr__ "Link to this definition")
: Called when the default attribute access fails with an [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError")
(either [`__getattribute__()`](#object.__getattribute__ "object.__getattribute__") raises... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
1a824b4b-f796-5081-976d-6619a26c6b0b | docs | python | python_datamodel | 311 | 82 | [`__getattr__()`](#object.__getattr__ "object.__getattr__") is not called. (This is an intentional asymmetry between
[`__getattr__()`](#object.__getattr__ "object.__getattr__") and [`__setattr__()`](#object.__setattr__ "object.__setattr__").) This is done both for efficiency
reasons and because otherwise [`... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
167950d6-f9df-5a18-8d25-f15fb6620a45 | docs | python | python_datamodel | 311 | 83 | class. If the class also defines [`__getattr__()`](#object.__getattr__ "object.__getattr__"), the latter will not be
called unless [`__getattribute__()`](#object.__getattribute__ "object.__getattribute__") either calls it explicitly or raises an
[`AttributeError`](../library/exceptions.html#AttributeError "... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
7b8075b4-d9ee-5894-952b-999841225834 | docs | python | python_datamodel | 311 | 84 | For certain sensitive attribute accesses, raises an
[auditing event](../library/sys.html#auditing) `object.__getattr__` with arguments
`obj` and `name`.
object.\_\_setattr\_\_(*self*, *name*, *value*)[¶](#object.__setattr__ "Link to this definition")
: Called when an attribute assignment is attempted. Th... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
f384932c-3761-5284-87aa-2a85549caf34 | docs | python | python_datamodel | 311 | 85 | : Like [`__setattr__()`](#object.__setattr__ "object.__setattr__") but for attribute deletion instead of assignment. This
should only be implemented if `del obj.name` is meaningful for the object.
For certain sensitive attribute deletions, raises an
[auditing event](../library/sys.html#auditing) `object.... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
d96604ff-a3b4-5f6d-9a40-7f9309aaa429 | docs | python | python_datamodel | 311 | 86 | computed value or raise an [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError"). If an attribute is
not found on a module object through the normal lookup, i.e.
[`object.__getattribute__()`](#object.__getattribute__ "object.__getattribute__"), then `__getattr__` is searched in
the module `__di... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
8d29d9ba-479e-5488-a564-ce6a86c0fe8b | docs | python | python_datamodel | 311 | 87 | ```None
import sys
from types import ModuleType
class VerboseModule(ModuleType):
def __repr__(self):
return f'Verbose {self.__name__}'
def __setattr__(self, attr, value):
print(f'Setting {attr}...')
super().__setattr__(attr, value)
sys.modules[__name__].__class__ = VerboseModule
```
... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
2776020c-420b-568d-aee8-571299f1205d | docs | python | python_datamodel | 311 | 88 | #### 3.3.2.2. Implementing Descriptors[¶](#implementing-descriptors "Link to this heading")
The following methods only apply when an instance of the class containing the
method (a so-called *descriptor* class) appears in an *owner* class (the
descriptor must be in either the owner’s class dictionary or in the class
di... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
d7cfa746-6ae6-5f5f-911a-cec04d5c316e | docs | python | python_datamodel | 311 | 89 | This method should return the computed attribute value or raise an
[`AttributeError`](../library/exceptions.html#AttributeError "AttributeError") exception.
[**PEP 252**](https://peps.python.org/pep-0252/) specifies that [`__get__()`](#object.__get__ "object.__get__") is callable with one or two
argume... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
c755fff4-b212-522a-9277-a74d80840f8a | docs | python | python_datamodel | 311 | 90 | descriptor to a “data descriptor”. See [Invoking Descriptors](#descriptor-invocation) for
more details.
object.\_\_delete\_\_(*self*, *instance*)[¶](#object.__delete__ "Link to this definition")
: Called to delete the attribute on an instance *instance* of the owner class.
Instances of descriptors may also ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
56e64349-2038-5170-92f4-cda06ea4a920 | docs | python | python_datamodel | 311 | 91 | #### 3.3.2.3. Invoking Descriptors[¶](#invoking-descriptors "Link to this heading")
In general, a descriptor is an object attribute with “binding behavior”, one
whose attribute access has been overridden by methods in the descriptor
protocol: [`__get__()`](#object.__get__ "object.__get__"), [`__set__()`](#object.__set... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
3d84565f-541d-5ca2-81fb-013e597c6dd8 | docs | python | python_datamodel | 311 | 92 | descriptor methods were defined and how they were called.
The starting point for descriptor invocation is a binding, `a.x`. How the
arguments are assembled depends on `a`:
Direct Call
: The simplest and least common call is when user code directly invokes a
descriptor method: `x.__get__(a)`.
Instance Binding
:... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
c4958534-76a0-57aa-be08-df197cdd2e89 | docs | python | python_datamodel | 311 | 93 | of [`__get__()`](#object.__get__ "object.__get__"), [`__set__()`](#object.__set__ "object.__set__") and
[`__delete__()`](#object.__delete__ "object.__delete__"). If it does not
define `__get__()`, then accessing the attribute will return the descriptor
object itself unless there is a value in the object’s instance dict... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
ef90adaa-2799-56da-9132-c95e939c73c0 | docs | python | python_datamodel | 311 | 94 | implemented as non-data descriptors. Accordingly, instances can redefine and
override methods. This allows individual instances to acquire behaviors that
differ from other instances of the same class.
The [`property()`](../library/functions.html#property "property") function is implemented as a data descriptor. Accord... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
cb25ed2d-564a-5beb-922c-a3d04568cd42 | docs | python | python_datamodel | 311 | 95 | : This class variable can be assigned a string, iterable, or sequence of
strings with variable names used by instances. *\_\_slots\_\_* reserves space
for the declared variables and prevents the automatic creation of
[`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__")
and *\_\_wea... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
56c4d924-3f6f-5b0e-b033-498a7965b694 | docs | python | python_datamodel | 311 | 96 | variables is desired, then add `'__dict__'` to the sequence of strings in
the *\_\_slots\_\_* declaration.
* Without a *\_\_weakref\_\_* variable for each instance, classes defining
*\_\_slots\_\_* do not support [`weak references`](../library/weakref.html#module-weakref "weakref: Support for weak references and ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
4f5c55ca-8205-553c-88e4-b71aeb601bdb | docs | python | python_datamodel | 311 | 97 | child classes. However, child subclasses will get a [`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__") and
*\_\_weakref\_\_* unless they also define *\_\_slots\_\_* (which should only
contain names of any *additional* slots).
* If a class defines a slot also defined in a base class, the inst... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
aba10717-60b2-5045-a77a-60798bf6438f | docs | python | python_datamodel | 311 | 98 | * Any non-string [iterable](../glossary.html#term-iterable) may be assigned to *\_\_slots\_\_*.
* If a [`dictionary`](../library/stdtypes.html#dict "dict") is used to assign *\_\_slots\_\_*, the dictionary
keys will be used as the slot names. The values of the dictionary can be used
to provide per-attribute docstri... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
2f8be98a-c259-5fc8-abe6-b58a428e23c8 | docs | python | python_datamodel | 311 | 99 | * If an [iterator](../glossary.html#term-iterator) is used for *\_\_slots\_\_* then a [descriptor](../glossary.html#term-descriptor) is
created for each
of the iterator’s values. However, the *\_\_slots\_\_* attribute will be an empty
iterator.
### 3.3.3. Customizing class creation[¶](#customizing-class-creation... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
5685315d-a999-5e13-a76b-fb87a750de2c | docs | python | python_datamodel | 311 | 100 | *cls* is then the new subclass. If defined as a normal instance method,
this method is implicitly converted to a class method.
Keyword arguments which are given to a new class are passed to
the parent class’s `__init_subclass__`. For compatibility with
other classes using `__init_subclass__`, one s... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
f6602092-803f-554e-a0ff-63b72c5f3df7 | docs | python | python_datamodel | 311 | 101 | The actual metaclass (rather than the explicit hint) can be accessed as
`type(cls)`.
New in version 3.6.
When a class is created, `type.__new__()` scans the class variables
and makes callbacks to those with a [`__set_name__()`](#object.__set_name__ "object.__set_name__") hook.
object.\_\_set\_name\_\_(*s... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
22732f24-3b10-5f46-b1f9-dc988d0d47bb | docs | python | python_datamodel | 311 | 102 | c.__set_name__(A, 'x') # Manually invoke the hook
```
See [Creating the class object](#class-object-creation) for more details.
New in version 3.6.
#### 3.3.3.1. Metaclasses[¶](#metaclasses "Link to this heading")
By default, classes are constructed using [`type()`](../library/functions.html#type ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
73f21a38-3071-54dc-92b2-1f28648e8ed6 | docs | python | python_datamodel | 311 | 103 | When a class definition is executed, the following steps occur:
* MRO entries are resolved;
* the appropriate metaclass is determined;
* the class namespace is prepared;
* the class body is executed;
* the class object is created.
#### 3.3.3.2. Resolving MRO entries[¶](#resolving-mro-entries "Link to this heading")
... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
9320f26d-929f-5399-9519-56a27f54e3b6 | docs | python | python_datamodel | 311 | 104 | [`types.resolve_bases()`](../library/types.html#types.resolve_bases "types.resolve_bases")
: Dynamically resolve bases that are not instances of [`type`](../library/functions.html#type "type").
[**PEP 560**](https://peps.python.org/pep-0560/)
: Core support for typing module and generic types.
#### 3.3.3.3. Deter... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
df6ca95f-0d5c-528c-96ec-32941c99bc5b | docs | python | python_datamodel | 311 | 105 | metaclass (if any) and the metaclasses (i.e. `type(cls)`) of all specified
base classes. The most derived metaclass is one which is a subtype of *all*
of these candidate metaclasses. If none of the candidate metaclasses meets
that criterion, then the class definition will fail with `TypeError`.
#### 3.3.3.4. Preparing... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
f1da26d8-f365-5aab-846b-d707fa38b0f9 | docs | python | python_datamodel | 311 | 106 | If the metaclass has no `__prepare__` attribute, then the class namespace
is initialised as an empty ordered mapping.
See also
[**PEP 3115**](https://peps.python.org/pep-3115/) - Metaclasses in Python 3000
: Introduced the `__prepare__` namespace hook
#### 3.3.3.5. Executing the class body[¶](#executing-the-class-... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
1404b925-d5a5-508a-bfa7-de0ac23d2cac | docs | python | python_datamodel | 311 | 107 | described in the next section.
#### 3.3.3.6. Creating the class object[¶](#creating-the-class-object "Link to this heading")
Once the class namespace has been populated by executing the class body,
the class object is created by calling
`metaclass(name, bases, namespace, **kwds)` (the additional keywords
passed here ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
92b223fa-5b1f-52f0-9fe5-a54437ab20a2 | docs | python | python_datamodel | 311 | 108 | **CPython implementation detail:** In CPython 3.6 and later, the `__class__` cell is passed to the metaclass
as a `__classcell__` entry in the class namespace. If present, this must
be propagated up to the `type.__new__` call in order for the class to be
initialised correctly.
Failing to do so will result in a [`Runtim... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
1841b1b1-ec3e-5747-9486-6aa54ed3fc0c | docs | python | python_datamodel | 311 | 109 | 3. The [`__init_subclass__()`](#object.__init_subclass__ "object.__init_subclass__") hook is called on the
immediate parent of the new class in its method resolution order.
After the class object is created, it is passed to the class decorators
included in the class definition (if any) and the resulting object is b... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
00c55287-9886-5cd6-8de8-cb2a3a798839 | docs | python | python_datamodel | 311 | 110 | #### 3.3.3.7. Uses for metaclasses[¶](#uses-for-metaclasses "Link to this heading")
The potential uses for metaclasses are boundless. Some ideas that have been
explored include enum, logging, interface checking, automatic delegation,
automatic property creation, proxies, frameworks, and automatic resource
locking/sync... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
319ce590-4db8-5fd9-a4a9-74defe9cc45d | docs | python | python_datamodel | 311 | 111 | class.\_\_instancecheck\_\_(*self*, *instance*)[¶](#class.__instancecheck__ "Link to this definition")
: Return true if *instance* should be considered a (direct or indirect)
instance of *class*. If defined, called to implement `isinstance(instance,
class)`.
class.\_\_subclasscheck\_\_(*self*, *subclass*)[¶]... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
5e3f36f2-9abb-5efb-bd00-4c11757cc78a | docs | python | python_datamodel | 311 | 112 | [`issubclass()`](../library/functions.html#issubclass "issubclass") behavior through [`__instancecheck__()`](#class.__instancecheck__ "class.__instancecheck__") and
[`__subclasscheck__()`](#class.__subclasscheck__ "class.__subclasscheck__"), with motivation for this functionality
in the context of adding Ab... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
25ce479e-6852-5d25-8039-e2c4620b311f | docs | python | python_datamodel | 311 | 113 | [**PEP 484**](https://peps.python.org/pep-0484/) - Type Hints
: Introducing Python’s framework for type annotations
[Generic Alias Types](../library/stdtypes.html#types-genericalias)
: Documentation for objects representing parameterized generic classes
[Generics](../library/typing.html#generics), [user-defined g... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
50252b0e-fed9-531c-b8e1-f256e1921e54 | docs | python | python_datamodel | 311 | 114 | method. As such, there is no need for it to be decorated with
[`@classmethod`](../library/functions.html#classmethod "classmethod") when it is defined.
#### 3.3.5.1. The purpose of *\_\_class\_getitem\_\_*[¶](#the-purpose-of-class-getitem "Link to this heading")
The purpose of [`__class_getitem__()`](#object.... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
6c5bca9a-67f8-5600-99d0-1148917f0292 | docs | python | python_datamodel | 311 | 115 | Custom implementations of [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__") on classes defined
outside of the standard library may not be understood by third-party
type-checkers such as mypy. Using `__class_getitem__()` on any class for
purposes other than type hinting is discouraged.
#### ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
66bd59fe-7b72-584f-b117-42583154815d | docs | python | python_datamodel | 311 | 116 | Presented with the [expression](../glossary.html#term-expression) `obj[x]`, the Python interpreter
follows something like the following process to decide whether
[`__getitem__()`](#object.__getitem__ "object.__getitem__") or [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__") should be
called:... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
5af99fa9-14fc-5dcd-a89a-d5d42daf6a54 | docs | python | python_datamodel | 311 | 117 | In Python, all classes are themselves instances of other classes. The class of
a class is known as that class’s [metaclass](../glossary.html#term-metaclass), and most classes have the
[`type`](../library/functions.html#type "type") class as their metaclass. [`type`](../library/functions.html#type "type") does not defin... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
09298b9f-7cad-50ab-9be5-6d6b2014415c | docs | python | python_datamodel | 311 | 118 | [`__getitem__()`](#object.__getitem__ "object.__getitem__"), subscribing the class may result in different
behaviour. An example of this can be found in the [`enum`](../library/enum.html#module-enum "enum: Implementation of an enumeration class.") module:
```None
>>> from enum import Enum
>>> class Menu(Enum):
... ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
2295c44a-e30d-5659-8143-61018daf30cf | docs | python | python_datamodel | 311 | 119 | being called instead of [`__getitem__()`](#object.__getitem__ "object.__getitem__")
### 3.3.6. Emulating callable objects[¶](#emulating-callable-objects "Link to this heading")
object.\_\_call\_\_(*self*[, *args...*])[¶](#object.__call__ "Link to this definition")
: Called when the instance is “called” as a fun... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
40e0696d-f6d0-564e-9a56-d8ad583fcde0 | docs | python | python_datamodel | 311 | 120 | either to emulate a sequence or to emulate a mapping; the difference is that for
a sequence, the allowable keys should be the integers *k* for which `0 <= k <
N` where *N* is the length of the sequence, or [`slice`](../library/functions.html#slice "slice") objects, which define a
range of items. It is also recommended ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
829a61c9-c275-53dd-bfbc-db7047d92493 | docs | python | python_datamodel | 311 | 121 | [`__getitem__()`](#object.__getitem__ "object.__getitem__"), [`__setitem__()`](#object.__setitem__ "object.__setitem__"),
[`__delitem__()`](#object.__delitem__ "object.__delitem__"), and `keys()`.
Mutable sequences should provide methods `append()`, `count()`,
`index()`, `extend()`, `insert()`, `pop()`, `remove()`,
`re... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
09406625-a762-5bfb-938a-ee5f1da4fdf5 | docs | python | python_datamodel | 311 | 122 | [`__contains__()`](#object.__contains__ "object.__contains__") method to allow efficient use of the `in`
operator; for
mappings, `in` should search the mapping’s keys; for sequences, it should
search through the values. It is further recommended that both mappings and
sequences implement the [`__iter__()`](#object.__it... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
8b5aad7c-6c46-5b4e-bfc5-7fec22472463 | docs | python | python_datamodel | 311 | 123 | **CPython implementation detail:** In CPython, the length is required to be at most [`sys.maxsize`](../library/sys.html#sys.maxsize "sys.maxsize").
If the length is larger than `sys.maxsize` some features (such as
[`len()`](../library/functions.html#len "len")) may raise [`OverflowError`](../library/excepti... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
ecec78f9-6c14-5629-a26e-7a893d5cf388 | docs | python | python_datamodel | 311 | 124 | `__length_hint__` method didn’t exist at all. This method is purely an
optimization and is never required for correctness.
New in version 3.4.
Note
Slicing is done exclusively with the following three methods. A call like
```None
a[1:2] = b
```
is translated to
```None
a[slice(1, 2, None)] = b
```
an... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
1dfec984-94b4-57ce-846e-a439609b2c87 | docs | python | python_datamodel | 311 | 125 | interpretation of negative values), [`IndexError`](../library/exceptions.html#IndexError "IndexError") should be raised. For
[mapping](../glossary.html#term-mapping) types, if *key* is missing (not in the container),
[`KeyError`](../library/exceptions.html#KeyError "KeyError") should be raised.
Note
... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
e371f390-ba7f-54d2-b207-95783597a5b0 | docs | python | python_datamodel | 311 | 126 | : Called to implement assignment to `self[key]`. Same note as for
[`__getitem__()`](#object.__getitem__ "object.__getitem__"). This should only be implemented for mappings if the
objects support changes to the values for keys, or if new keys can be added, or
for sequences if elements can be replaced. The ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
b2b1d7af-3c18-51b7-8e0e-56925d81c695 | docs | python | python_datamodel | 311 | 127 | object.\_\_missing\_\_(*self*, *key*)[¶](#object.__missing__ "Link to this definition")
: Called by [`dict`](../library/stdtypes.html#dict "dict").[`__getitem__()`](#object.__getitem__ "object.__getitem__") to implement `self[key]` for dict subclasses
when key is not in the dictionary.
object.\_\_iter\_\_(*self*... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
a88b582c-a328-57ff-80f9-827e832701b7 | docs | python | python_datamodel | 311 | 128 | If the [`__reversed__()`](#object.__reversed__ "object.__reversed__") method is not provided, the [`reversed()`](../library/functions.html#reversed "reversed")
built-in will fall back to using the sequence protocol ([`__len__()`](#object.__len__ "object.__len__") and
[`__getitem__()`](#object.__getitem__ "o... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
87d6aaec-5abc-57d3-8482-3ecffaf85209 | docs | python | python_datamodel | 311 | 129 | object.\_\_contains\_\_(*self*, *item*)[¶](#object.__contains__ "Link to this definition")
: Called to implement membership test operators. Should return true if *item*
is in *self*, false otherwise. For mapping objects, this should consider the
keys of the mapping rather than the values or the key-item pairs... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
32de6156-f78d-576d-8693-5792752c1960 | docs | python | python_datamodel | 311 | 130 | number implemented (e.g., bitwise operations for non-integral numbers) should be
left undefined.
object.\_\_add\_\_(*self*, *other*)[¶](#object.__add__ "Link to this definition")
object.\_\_sub\_\_(*self*, *other*)[¶](#object.__sub__ "Link to this definition")
object.\_\_mul\_\_(*self*, *other*)[¶](#object.__mul__ "... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
439f487a-3793-5568-9889-892abf9b078e | docs | python | python_datamodel | 311 | 131 | object.\_\_rshift\_\_(*self*, *other*)[¶](#object.__rshift__ "Link to this definition")
object.\_\_and\_\_(*self*, *other*)[¶](#object.__and__ "Link to this definition")
object.\_\_xor\_\_(*self*, *other*)[¶](#object.__xor__ "Link to this definition")
object.\_\_or\_\_(*self*, *other*)[¶](#object.__or__ "Link to thi... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
ad07c080-cfa1-59de-a44d-5beff5c840fb | docs | python | python_datamodel | 311 | 132 | [`__floordiv__()`](#object.__floordiv__ "object.__floordiv__") and [`__mod__()`](#object.__mod__ "object.__mod__"); it should not be related to
[`__truediv__()`](#object.__truediv__ "object.__truediv__"). Note that [`__pow__()`](#object.__pow__ "object.__pow__") should be defined to accept
an optional third... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
d4a3353d-4baa-5fd8-936d-4255ea92b2ad | docs | python | python_datamodel | 311 | 133 | object.\_\_rtruediv\_\_(*self*, *other*)[¶](#object.__rtruediv__ "Link to this definition")
object.\_\_rfloordiv\_\_(*self*, *other*)[¶](#object.__rfloordiv__ "Link to this definition")
object.\_\_rmod\_\_(*self*, *other*)[¶](#object.__rmod__ "Link to this definition")
object.\_\_rdivmod\_\_(*self*, *other*)[¶](#obj... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
6546d50c-866e-5c59-b3c2-35baca1116fe | docs | python | python_datamodel | 311 | 134 | (`+`, `-`, `*`, `@`, `/`, `//`, `%`, [`divmod()`](../library/functions.html#divmod "divmod"),
[`pow()`](../library/functions.html#pow "pow"), `**`, `<<`, `>>`, `&`, `^`, `|`) with reflected
(swapped) operands. These functions are only called if the left operand does
not support the corresponding operati... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
a438b3a9-1e03-5a6a-ad6d-fde4d80ed5dd | docs | python | python_datamodel | 311 | 135 | that subclass provides a different implementation of the reflected method
for the operation, this method will be called before the left operand’s
non-reflected method. This behavior allows subclasses to override their
ancestors’ operations.
object.\_\_iadd\_\_(*self*, *other*)[¶](#object.__iadd__ "Link... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
db5d2621-f9b8-542a-8ba1-992857f27702 | docs | python | python_datamodel | 311 | 136 | object.\_\_ilshift\_\_(*self*, *other*)[¶](#object.__ilshift__ "Link to this definition")
object.\_\_irshift\_\_(*self*, *other*)[¶](#object.__irshift__ "Link to this definition")
object.\_\_iand\_\_(*self*, *other*)[¶](#object.__iand__ "Link to this definition")
object.\_\_ixor\_\_(*self*, *other*)[¶](#object.__ixo... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
3ee37b77-decf-50f5-8692-6852d6996bd2 | docs | python | python_datamodel | 311 | 137 | is an instance of a class with an [`__iadd__()`](#object.__iadd__ "object.__iadd__") method, `x += y` is
equivalent to `x = x.__iadd__(y)` . If [`__iadd__()`](#object.__iadd__ "object.__iadd__") does not exist, or if `x.__iadd__(y)`
returns `NotImplemented`, `x.__add__(y)` and
`y.__radd__(x)` are consid... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
699a0cf6-c149-58f3-baa7-47a581e3f2d9 | docs | python | python_datamodel | 311 | 138 | : Called to implement the unary arithmetic operations (`-`, `+`, [`abs()`](../library/functions.html#abs "abs")
and `~`).
object.\_\_complex\_\_(*self*)[¶](#object.__complex__ "Link to this definition")
object.\_\_int\_\_(*self*)[¶](#object.__int__ "Link to this definition")
object.\_\_float\_\_(*self*)[¶](#ob... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
19670a1e-39c3-56f0-a728-b4e5045dc5e0 | docs | python | python_datamodel | 311 | 139 | slicing, or in the built-in [`bin()`](../library/functions.html#bin "bin"), [`hex()`](../library/functions.html#hex "hex") and [`oct()`](../library/functions.html#oct "oct")
functions). Presence of this method indicates that the numeric object is
an integer type. Must return an integer.
If [`__int__()`... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
7096658c-74c9-5224-bee2-8096c29d736f | docs | python | python_datamodel | 311 | 140 | object.\_\_ceil\_\_(*self*)[¶](#object.__ceil__ "Link to this definition")
: Called to implement the built-in function [`round()`](../library/functions.html#round "round") and [`math`](../library/math.html#module-math "math: Mathematical functions (sin() etc.).")
functions [`trunc()`](../library/math.html#math.tr... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
331e5d2e-3dda-56d8-a3a4-712a2448ecee | docs | python | python_datamodel | 311 | 141 | Changed in version 3.11: The delegation of [`int()`](../library/functions.html#int "int") to [`__trunc__()`](#object.__trunc__ "object.__trunc__") is deprecated.
### 3.3.9. With Statement Context Managers[¶](#with-statement-context-managers "Link to this heading")
A *context manager* is an object that defines the... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
d9f7b704-f4c3-57ac-b00d-0d2b7b3f7cc3 | docs | python | python_datamodel | 311 | 142 | object.\_\_enter\_\_(*self*)[¶](#object.__enter__ "Link to this definition")
: Enter the runtime context related to this object. The [`with`](compound_stmts.html#with) statement
will bind this method’s return value to the target(s) specified in the
`as` clause of the statement, if any.
object.\_\_exit\_\_(*s... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
702844fa-9c8f-577b-9540-817d613685bc | docs | python | python_datamodel | 311 | 143 | Note that [`__exit__()`](#object.__exit__ "object.__exit__") methods should not reraise the passed-in exception;
this is the caller’s responsibility.
See also
[**PEP 343**](https://peps.python.org/pep-0343/) - The “with” statement
: The specification, background, and examples for the Python [`with`](compoun... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
adae8781-9ad6-5da5-bf5a-153bf711747c | docs | python | python_datamodel | 311 | 144 | used in a class pattern with positional arguments, each positional argument will
be converted into a keyword argument, using the corresponding value in
*\_\_match\_args\_\_* as the keyword. The absence of this attribute is equivalent to
setting it to `()`.
For example, if `MyClass.__match_args__` is `(... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
aebe1827-11f6-558a-aa23-007a9fb96cee | docs | python | python_datamodel | 311 | 145 | ### 3.3.11. Special method lookup[¶](#special-method-lookup "Link to this heading")
For custom classes, implicit invocations of special methods are only guaranteed
to work correctly if defined on an object’s type, not in the object’s instance
dictionary. That behaviour is the reason why the following code raises an
ex... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
b38d70ca-bc2b-5f49-9e61-8dce274df5a5 | docs | python | python_datamodel | 311 | 146 | Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__hash__' of 'int' object needs an argument
```
Incorrectly attempting to invoke an unbound method of a class in this way is
sometimes referred to as ‘metaclass confusion’, and is avoided by bypassing
the instance when look... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
0e834c28-2bdf-57eb-b3ab-d5c5ff798179 | docs | python | python_datamodel | 311 | 147 | ... print("Class getattribute invoked")
... return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__() # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c) # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c) ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
f028dd4c-48d1-574c-a999-a7144edd8463 | docs | python | python_datamodel | 311 | 148 | [Coroutine objects](../glossary.html#term-coroutine) returned from [`async def`](compound_stmts.html#async-def) functions
are awaitable.
Note
The [generator iterator](../glossary.html#term-generator-iterator) objects returned from generators
decorated with [`types.coroutine()`](../library/types.html#types.coroutine "... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
00cb8c8b-5b9c-58e3-964e-ed719fdabbe5 | docs | python | python_datamodel | 311 | 149 | specific to the implementation of the asynchronous execution framework
(e.g. [`asyncio`](../library/asyncio.html#module-asyncio "asyncio: Asynchronous I/O.")) that will be managing the [awaitable](../glossary.html#term-awaitable) object.
New in version 3.5.
See also
[**PEP 492**](https://peps.python.org/pep-... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
465390ca-a44e-52cb-9796-e9dfb12dd0c0 | docs | python | python_datamodel | 311 | 150 | coroutine raises an exception, it is propagated by the iterator. Coroutines
should not directly raise unhandled [`StopIteration`](../library/exceptions.html#StopIteration "StopIteration") exceptions.
Coroutines also have the methods listed below, which are analogous to
those of generators (see [Generator-iterator meth... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
7e2a23a7-6dff-5cf3-ac5d-45297d0fe7fc | docs | python | python_datamodel | 311 | 151 | the coroutine to suspend. The result (return value,
[`StopIteration`](../library/exceptions.html#StopIteration "StopIteration"), or other exception) is the same as when
iterating over the `__await__()` return value, described above.
coroutine.throw(*value*)[¶](#coroutine.throw "Link to this definition")
c... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
17dad75d-b414-5d0e-bff4-64819877e645 | docs | python | python_datamodel | 311 | 152 | coroutine.close()[¶](#coroutine.close "Link to this definition")
: Causes the coroutine to clean itself up and exit. If the coroutine
is suspended, this method first delegates to the [`close()`](expressions.html#generator.close "generator.close")
method of the iterator that caused the coroutine to suspend, if... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
3715c6c8-76f6-5eb3-a94a-00b887f1c4dc | docs | python | python_datamodel | 311 | 153 | object.\_\_aiter\_\_(*self*)[¶](#object.__aiter__ "Link to this definition")
: Must return an *asynchronous iterator* object.
object.\_\_anext\_\_(*self*)[¶](#object.__anext__ "Link to this definition")
: Must return an *awaitable* resulting in a next value of the iterator. Should
raise a [`StopAsyncIteration`... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
ef87ff41-8ff4-51d8-a703-ffd5d21fdf01 | docs | python | python_datamodel | 311 | 154 | Starting with Python 3.7, [`__aiter__()`](#object.__aiter__ "object.__aiter__") must return an
asynchronous iterator object. Returning anything else
will result in a [`TypeError`](../library/exceptions.html#TypeError "TypeError") error.
### 3.4.4. Asynchronous Context Managers[¶](#asynchronous-context-managers "Link t... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
b1a47856-684b-5647-8095-8f4fa5cfa28a | docs | python | python_datamodel | 311 | 155 | difference being that it must return an *awaitable*.
An example of an asynchronous context manager class:
```None
class AsyncContextManager:
async def __aenter__(self):
await log('entering context')
async def __aexit__(self, exc_type, exc, tb):
await log('exiting context')
```
New in ver... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
4b63043c-8cde-54c4-9e54-f28d3f8c60e5 | docs | python | python_datamodel | 311 | 156 |
[[3](#id14)]
“Does not support” here means that the class has no such method, or
the method returns [`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented"). Do not set the method to
`None` if you want to force fallback to the right operand’s reflected
method—that will instead have the opposite ... | large_dataset/raw/docs/python/datamodel/datamodel_311.md |
dd2ab409-7900-591a-aef5-dc6e763acec5 | docs | python | python_datamodel | 312 | 0 | # 3.Data model¶
Source: https://docs.python.org/3.12/reference/datamodel.html
Version: 3.12
## 3.1. Objects, values and types[¶](#objects-values-and-types "Link to this heading")
*Objects* are Python’s abstraction for data. All data in a Python program
is represented by objects or by relations between objects. (In a... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
758924e5-f907-534b-9f8e-f5d18f661575 | docs | python | python_datamodel | 312 | 1 | it have a length?”) and also defines the possible values for objects of that
type. The [`type()`](../library/functions.html#type "type") function returns an object’s type (which is an object
itself). Like its identity, an object’s *type* is also unchangeable.
[[1]](#id18)
The *value* of some objects can change. Object... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
1ea198e0-ca10-5f7a-9da3-326ca0046d94 | docs | python | python_datamodel | 312 | 2 | Objects are never explicitly destroyed; however, when they become unreachable
they may be garbage-collected. An implementation is allowed to postpone garbage
collection or omit it altogether — it is a matter of implementation quality
how garbage collection is implemented, as long as no objects are collected that
are st... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
55614764-743b-5723-96ec-7991d006e351 | docs | python | python_datamodel | 312 | 3 | Note that the use of the implementation’s tracing or debugging facilities may
keep objects alive that would normally be collectable. Also note that catching
an exception with a [`try`](compound_stmts.html#try)…[`except`](compound_stmts.html#except) statement may keep
objects alive.
Some objects contain references to “... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.