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 |
|---|---|---|---|---|---|---|---|
a80ef323-7be8-5983-a9ab-bb7345f00831 | docs | python | python_datamodel | 38 | 76 | [**PEP 562**](https://www.python.org/dev/peps/pep-0562) - Module \_\_getattr\_\_ and \_\_dir\_\_
: Describes the `__getattr__` and `__dir__` functions on modules.
#### 3.3.2.2. Implementing Descriptors[¶](#implementing-descriptors "Permalink to this headline")
The following methods only apply when an instance of th... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
86d58ec5-a424-543f-8a2e-441a3267e68f | docs | python | python_datamodel | 38 | 77 | *owner* argument is the owner class, while *instance* is the instance that
the attribute was accessed through, or `None` when the attribute is
accessed through the *owner*.
This method should return the computed attribute value or raise an
[`AttributeError`](../library/exceptions.html#AttributeErro... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
d6177efb-a392-5dfc-b0dd-01043c70b625 | docs | python | python_datamodel | 38 | 78 | : Called to set the attribute on an instance *instance* of the owner class to a
new value, *value*.
Note, adding [`__set__()`](#object.__set__ "object.__set__") or [`__delete__()`](#object.__delete__ "object.__delete__") changes the kind of
descriptor to a “data descriptor”. See [Invoking Descriptors](#d... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
9782a8b1-f132-5dd0-ab6a-2d4824fe96d2 | docs | python | python_datamodel | 38 | 79 | the appropriate parameters when a descriptor is added to a class after
initial creation:
```None
class A:
pass
descr = custom_descriptor()
A.attr = descr
descr.__set_name__(A, 'attr')
```
See [Creating the class object](#class-object-creation) for more details.
New in v... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
217ca48f-ecdf-5b7c-b400-868056155053 | docs | python | python_datamodel | 38 | 80 | #### 3.3.2.3. Invoking Descriptors[¶](#invoking-descriptors "Permalink to this headline")
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... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
55d98c7b-d31b-5b52-a6e6-db416399476f | docs | python | python_datamodel | 38 | 81 | 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_38.md |
9795bffb-2369-5829-b4a5-170a952753db | docs | python | python_datamodel | 38 | 82 | which descriptor methods are defined. A descriptor can define any combination
of [`__get__()`](#object.__get__ "object.__get__"), [`__set__()`](#object.__set__ "object.__set__") and [`__delete__()`](#object.__delete__ "object.__delete__"). If it does not
define [`__get__()`](#object.__get__ "object.__get__"), then acce... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
c340c8ff-7986-5598-a3eb-9179176345c4 | docs | python | python_datamodel | 38 | 83 | [`__set__()`](#object.__set__ "object.__set__") and [`__get__()`](#object.__get__ "object.__get__") defined always override a redefinition in an
instance dictionary. In contrast, non-data descriptors can be overridden by
instances.
Python methods (including [`staticmethod()`](../library/functions.html#staticmethod "st... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
520b01a9-ef36-5fd7-9059-109fbdfd0a3c | docs | python | python_datamodel | 38 | 84 | (unless explicitly declared in *\_\_slots\_\_* or available in a parent.)
The space saved over using *\_\_dict\_\_* can be significant.
Attribute lookup speed can be significantly improved as well.
`object.``__slots__`[¶](#object.__slots__ "Permalink to this definition")
: This class variable can be assigned a stri... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a61db51a-b1b5-5500-95fc-9c05ca8b18ef | docs | python | python_datamodel | 38 | 85 | variable name raises [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError"). If dynamic assignment of new
variables is desired, then add `'__dict__'` to the sequence of strings in
the *\_\_slots\_\_* declaration.
* Without a *\_\_weakref\_\_* variable for each instance, classes defining
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6180ad14-df5e-53b6-a766-35dcdc3c4228 | docs | python | python_datamodel | 38 | 86 | child classes. However, child subclasses will get a *\_\_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 instance variable
defined by the base class slot is inaccess... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
62cd096c-550a-5566-b02f-e4975c47a968 | docs | python | python_datamodel | 38 | 87 | * *\_\_class\_\_* assignment works only if both classes have the same *\_\_slots\_\_*.
* Multiple inheritance with multiple slotted parent classes can be used,
but only one parent is allowed to have attributes created by slots
(the other bases must have empty slot layouts) - violations raise
[`TypeError`](../libr... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
9fd265a8-baaf-534a-8f6d-16c1c363f28c | docs | python | python_datamodel | 38 | 88 | class defining the method.
*classmethod* `object.``__init_subclass__`(*cls*)[¶](#object.__init_subclass__ "Permalink to this definition")
: This method is called whenever the containing class is subclassed.
*cls* is then the new subclass. If defined as a normal instance method,
this method is implicitly conv... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
b72ffb93-aa5e-522d-a2c5-a5741e331dca | docs | python | python_datamodel | 38 | 89 | nothing, but raises an error if it is called with any arguments.
Note
The metaclass hint `metaclass` is consumed by the rest of the type
machinery, and is never passed to `__init_subclass__` implementations.
The actual metaclass (rather than the explicit hint) can be accessed as
`type(cls)`.
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8ba0c855-a4fe-542f-b795-91249adf1151 | docs | python | python_datamodel | 38 | 90 | pass
```
Any other keyword arguments that are specified in the class definition are
passed through to all metaclass operations described below.
When a class definition is executed, the following steps occur:
* MRO entries are resolved;
* the appropriate metaclass is determined;
* the class namespace is prepared;... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
7a5a70d6-83ce-59af-b7c6-99abf98f5365 | docs | python | python_datamodel | 38 | 91 | #### 3.3.3.3. Determining the appropriate metaclass[¶](#determining-the-appropriate-metaclass "Permalink to this headline")
The appropriate metaclass for a class definition is determined as follows:
* if no bases and no explicit metaclass are given, then [`type()`](../library/functions.html#type "type") is used;
* if... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
953d0eca-9d23-5db3-b06d-4c4b09afa9d2 | docs | python | python_datamodel | 38 | 92 | #### 3.3.3.4. Preparing the class namespace[¶](#preparing-the-class-namespace "Permalink to this headline")
Once the appropriate metaclass has been identified, then the class namespace
is prepared. If the metaclass has a `__prepare__` attribute, it is called
as `namespace = metaclass.__prepare__(name, bases, **kwds)` ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6b2f7620-25e2-5469-bed1-d6e9160ebefb | docs | python | python_datamodel | 38 | 93 | #### 3.3.3.5. Executing the class body[¶](#executing-the-class-body "Permalink to this headline")
The class body is executed (approximately) as
`exec(body, globals(), namespace)`. The key difference from a normal
call to [`exec()`](../library/functions.html#exec "exec") is that lexical scoping allows the class body (i... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
827cf469-214f-52d2-b765-d6a70a79c214 | docs | python | python_datamodel | 38 | 94 | `metaclass(name, bases, namespace, **kwds)` (the additional keywords
passed here are the same as those passed to `__prepare__`).
This class object is the one that will be referenced by the zero-argument
form of [`super()`](../library/functions.html#super "super"). `__class__` is an implicit closure reference
created b... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
399cca9b-26f0-5662-8518-dadd4d552f36 | docs | python | python_datamodel | 38 | 95 | Failing to do so will result in a [`RuntimeError`](../library/exceptions.html#RuntimeError "RuntimeError") in Python 3.8.
When using the default metaclass [`type`](../library/functions.html#type "type"), or any metaclass that ultimately
calls `type.__new__`, the following additional customisation steps are
invoked aft... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
c1ed4d26-9e73-5187-9e9e-ebe43e4415ba | docs | python | python_datamodel | 38 | 96 | in the local namespace as the defined class.
When a new class is created by `type.__new__`, the object provided as the
namespace parameter is copied to a new ordered mapping and the original
object is discarded. The new copy is wrapped in a read-only proxy, which
becomes the [`__dict__`](../library/stdtypes.html#objec... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
f8397872-a27a-59a9-bf49-d39020a4b1aa | docs | python | python_datamodel | 38 | 97 | ### 3.3.4. Customizing instance and subclass checks[¶](#customizing-instance-and-subclass-checks "Permalink to this headline")
The following methods are used to override the default behavior of the
[`isinstance()`](../library/functions.html#isinstance "isinstance") and [`issubclass()`](../library/functions.html#issubc... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
f47f0858-2efb-5503-bf74-53d81e57d16c | docs | python | python_datamodel | 38 | 98 | : Return true if *subclass* should be considered a (direct or indirect)
subclass of *class*. If defined, called to implement `issubclass(subclass,
class)`.
Note that these methods are looked up on the type (metaclass) of a class. They
cannot be defined as class methods in the actual class. This is consistent... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
f273697e-6f43-5c95-94e9-2797ab52711b | docs | python | python_datamodel | 38 | 99 | in the context of adding Abstract Base Classes (see the [`abc`](../library/abc.html#module-abc "abc: Abstract base classes according to :pep:`3119`.")
module) to the language.
### 3.3.5. Emulating generic types[¶](#emulating-generic-types "Permalink to this headline")
One can implement the generic class synta... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
cbb36276-1046-5a10-bdf2-4adf26c35ee3 | docs | python | python_datamodel | 38 | 100 | [**PEP 560**](https://www.python.org/dev/peps/pep-0560) - Core support for typing module and generic types
### 3.3.6. Emulating callable objects[¶](#emulating-callable-objects "Permalink to this headline")
`object.``__call__`(*self*[, *args...*])[¶](#object.__call__ "Permalink to this definition")
: Called when the... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a28fa438-d403-583c-bf06-30128cf56261 | docs | python | python_datamodel | 38 | 101 | N` where *N* is the length of the sequence, or slice objects, which define a
range of items. It is also recommended that mappings provide the methods
`keys()`, `values()`, `items()`, `get()`, `clear()`,
`setdefault()`, `pop()`, `popitem()`, `copy()`, and
`update()` behaving similar to those for Python’s standard dictio... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
ece659ff-2351-5bab-b774-c1b0911335a2 | docs | python | python_datamodel | 38 | 102 | `reverse()` and `sort()`, like Python standard list objects. Finally,
sequence types should implement addition (meaning concatenation) and
multiplication (meaning repetition) by defining the methods [`__add__()`](#object.__add__ "object.__add__"),
[`__radd__()`](#object.__radd__ "object.__radd__"), [`__iadd__()`](#obje... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
ab8c6982-8f0f-5e60-adf2-2cf0d7813233 | docs | python | python_datamodel | 38 | 103 | through the container; for mappings, [`__iter__()`](#object.__iter__ "object.__iter__") should iterate
through the object’s keys; for sequences, it should iterate through the values.
`object.``__len__`(*self*)[¶](#object.__len__ "Permalink to this definition")
: Called to implement the built-in function [`len()`](..... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
f99e7c95-70c5-551d-ad4a-b5b226de3723 | docs | python | python_datamodel | 38 | 104 | [`len()`](../library/functions.html#len "len")) may raise [`OverflowError`](../library/exceptions.html#OverflowError "OverflowError"). To prevent raising
`OverflowError` by truth value testing, an object must define a
[`__bool__()`](#object.__bool__ "object.__bool__") method.
`object.``__length_hint__`(*se... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
aa3f2f9e-a3de-517a-8db2-2a2bba0db8c7 | docs | python | python_datamodel | 38 | 105 | 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
```
and so forth. Missing slice items are always filled in with `None`.
`object.``__getitem__`(*self*, *key*)[¶](#object.__getitem__ "Permalink to this definition")
: ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
82c87bad-c34f-51b3-a31d-0b553e7039b3 | docs | python | python_datamodel | 38 | 106 | in the container), [`KeyError`](../library/exceptions.html#KeyError "KeyError") should be raised.
Note
[`for`](compound_stmts.html#for) loops expect that an [`IndexError`](../library/exceptions.html#IndexError "IndexError") will be raised for illegal
indexes to allow proper detection of the end of the... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
57bfefba-6415-56a6-91e9-c6337768283f | docs | python | python_datamodel | 38 | 107 | : Called to implement deletion of `self[key]`. Same note as for
[`__getitem__()`](#object.__getitem__ "object.__getitem__"). This should only be implemented for mappings if the
objects support removal of keys, or for sequences if elements can be removed
from the sequence. The same exceptions should be rai... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
201f57e1-38bb-5849-87ae-46ff4078687c | docs | python | python_datamodel | 38 | 108 | container. For mappings, it should iterate over the keys of the container.
Iterator objects also need to implement this method; they are required to return
themselves. For more information on iterator objects, see [Iterator Types](../library/stdtypes.html#typeiter).
`object.``__reversed__`(*self*)[¶](#obj... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
23de5c61-47ba-5252-b64a-edab5c7b3fc3 | docs | python | python_datamodel | 38 | 109 | only provide [`__reversed__()`](#object.__reversed__ "object.__reversed__") if they can provide an implementation
that is more efficient than the one provided by [`reversed()`](../library/functions.html#reversed "reversed").
The membership test operators ([`in`](expressions.html#in) and [`not in`](expressions.... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
3f1cc00d-0f02-5779-bbc9-86459826f60c | docs | python | python_datamodel | 38 | 110 | tries iteration via [`__iter__()`](#object.__iter__ "object.__iter__"), then the old sequence iteration
protocol via [`__getitem__()`](#object.__getitem__ "object.__getitem__"), see [this section in the language
reference](expressions.html#membership-test-details).
### 3.3.8. Emulating numeric types[¶](#em... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
84f22238-29ff-5cdd-bca3-56ec6885c5a9 | docs | python | python_datamodel | 38 | 111 | `object.``__truediv__`(*self*, *other*)[¶](#object.__truediv__ "Permalink to this definition")
`object.``__floordiv__`(*self*, *other*)[¶](#object.__floordiv__ "Permalink to this definition")
`object.``__mod__`(*self*, *other*)[¶](#object.__mod__ "Permalink to this definition")
`object.``__divmod__`(*self*, *other*)... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
db500958-f5f1-5898-b839-c9e307d5a3b3 | docs | python | python_datamodel | 38 | 112 | (`+`, `-`, `*`, `@`, `/`, `//`, `%`, [`divmod()`](../library/functions.html#divmod "divmod"),
[`pow()`](../library/functions.html#pow "pow"), `**`, `<<`, `>>`, `&`, `^`, `|`). For instance, to
evaluate the expression `x + y`, where *x* is an instance of a class that
has an [`__add__()`](#object.__add__ ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
55716e45-c3b8-5f93-a463-3e631f094b62 | docs | python | python_datamodel | 38 | 113 | arguments, it should return `NotImplemented`.
`object.``__radd__`(*self*, *other*)[¶](#object.__radd__ "Permalink to this definition")
`object.``__rsub__`(*self*, *other*)[¶](#object.__rsub__ "Permalink to this definition")
`object.``__rmul__`(*self*, *other*)[¶](#object.__rmul__ "Permalink to this definition")
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
589cc266-6bd2-5389-a8e5-cb0a72c2c1c8 | docs | python | python_datamodel | 38 | 114 | `object.``__rrshift__`(*self*, *other*)[¶](#object.__rrshift__ "Permalink to this definition")
`object.``__rand__`(*self*, *other*)[¶](#object.__rand__ "Permalink to this definition")
`object.``__rxor__`(*self*, *other*)[¶](#object.__rxor__ "Permalink to this definition")
`object.``__ror__`(*self*, *other*)[¶](#obje... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
3e0554c1-386b-5a3a-ac47-34fd437886b2 | docs | python | python_datamodel | 38 | 115 | is called if `x.__sub__(y)` returns *NotImplemented*.
Note that ternary [`pow()`](../library/functions.html#pow "pow") will not try calling [`__rpow__()`](#object.__rpow__ "object.__rpow__") (the
coercion rules would become too complicated).
Note
If the right operand’s type is a subclass of the l... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
40b45130-c4a9-5847-9d25-98f554a37dbb | docs | python | python_datamodel | 38 | 116 | `object.``__itruediv__`(*self*, *other*)[¶](#object.__itruediv__ "Permalink to this definition")
`object.``__ifloordiv__`(*self*, *other*)[¶](#object.__ifloordiv__ "Permalink to this definition")
`object.``__imod__`(*self*, *other*)[¶](#object.__imod__ "Permalink to this definition")
`object.``__ipow__`(*self*, *oth... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
2afdbb0d-4172-5b16-8eb2-849a0b34e9d0 | docs | python | python_datamodel | 38 | 117 | `>>=`, `&=`, `^=`, `|=`). These methods should attempt to do the
operation in-place (modifying *self*) and return the result (which could be,
but does not have to be, *self*). If a specific method is not defined, the
augmented assignment falls back to the normal methods. For instance, if *x*
is an i... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8dddb482-e08f-5fc0-a997-8bba70a45c76 | docs | python | python_datamodel | 38 | 118 | defines [`__ipow__()`](#object.__ipow__ "object.__ipow__") but returns `NotImplemented` would fail to
fall back to `x.__pow__(y)` and `y.__rpow__(x)`. This bug is fixed
in Python 3.10.
`object.``__neg__`(*self*)[¶](#object.__neg__ "Permalink to this definition")
`object.``__pos__`(*self*)[¶](#object.__pos... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
83cba734-6a85-5481-8c8f-4e1db143c0f7 | docs | python | python_datamodel | 38 | 119 | [`int()`](../library/functions.html#int "int") and [`float()`](../library/functions.html#float "float"). Should return a value
of the appropriate type.
`object.``__index__`(*self*)[¶](#object.__index__ "Permalink to this definition")
: Called to implement [`operator.index()`](../library/operator.html#operato... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e27ed9cd-cae0-561a-854c-a7268a880bb4 | docs | python | python_datamodel | 38 | 120 | defined then corresponding built-in functions [`int()`](../library/functions.html#int "int"), [`float()`](../library/functions.html#float "float")
and [`complex()`](../library/functions.html#complex "complex") fall back to [`__index__()`](#object.__index__ "object.__index__").
`object.``__round__`(*self*[, *nd... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
eee8366b-1752-553a-9ccb-f9b79133ab51 | docs | python | python_datamodel | 38 | 121 | Unless *ndigits* is passed to `__round__()` all these methods should
return the value of the object truncated to an [`Integral`](../library/numbers.html#numbers.Integral "numbers.Integral")
(typically an [`int`](../library/functions.html#int "int")).
The built-in function [`int()`](../library/functions... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
0e001722-2fdd-5492-b52b-8da960bcb235 | docs | python | python_datamodel | 38 | 122 | `with` statement (described in section [The with statement](compound_stmts.html#with)), but can also be
used by directly invoking their methods.
Typical uses of context managers include saving and restoring various kinds of
global state, locking and unlocking resources, closing opened files, etc.
For more information... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
7de09d6d-87d1-54af-8459-fda5d66c5b69 | docs | python | python_datamodel | 38 | 123 | without an exception, all three arguments will be [`None`](../library/constants.html#None "None").
If an exception is supplied, and the method wishes to suppress the exception
(i.e., prevent it from being propagated), it should return a true value.
Otherwise, the exception will be processed normally up... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
b446cb14-f982-5281-81c5-59e70c20dac2 | docs | python | python_datamodel | 38 | 124 | dictionary. That behaviour is the reason why the following code raises an
exception:
```None
>>> class C:
... pass
...
>>> c = C()
>>> c.__len__ = lambda: 5
>>> len(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'C' has no len()
```
The rationale behind this ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
03b3f7ce-6713-5535-bde5-ed624119b121 | docs | python | python_datamodel | 38 | 125 | sometimes referred to as ‘metaclass confusion’, and is avoided by bypassing
the instance when looking up special methods:
```None
>>> type(1).__hash__(1) == hash(1)
True
>>> type(int).__hash__(int) == hash(int)
True
```
In addition to bypassing any instance attributes in the interest of
correctness, implicit special ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
7fe76cb1-d734-5e7f-8e38-597fbbad4599 | docs | python | python_datamodel | 38 | 126 | >>> type(c).__len__(c) # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c) # Implicit lookup
10
```
Bypassing the [`__getattribute__()`](#object.__getattribute__ "object.__getattribute__") machinery in this fashion
provides significant scope for speed optimisations wit... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e529ec24-1d20-5720-9a21-70c745fab210 | docs | python | python_datamodel | 38 | 127 | The [generator iterator](../glossary.html#term-generator-iterator) objects returned from generators
decorated with [`types.coroutine()`](../library/types.html#types.coroutine "types.coroutine") or [`asyncio.coroutine()`](../library/asyncio-task.html#asyncio.coroutine "asyncio.coroutine")
are also awaitable, but they do... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
63c01425-9089-587d-a62e-3e2602ddd841 | docs | python | python_datamodel | 38 | 128 | ### 3.4.2. Coroutine Objects[¶](#coroutine-objects "Permalink to this headline")
[Coroutine objects](../glossary.html#term-coroutine) are [awaitable](../glossary.html#term-awaitable) objects.
A coroutine’s execution can be controlled by calling [`__await__()`](#object.__await__ "object.__await__") and
iterating over t... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
d0ccd242-580b-5e1e-8bab-e04fc366d31d | docs | python | python_datamodel | 38 | 129 | Changed in version 3.5.2: It is a [`RuntimeError`](../library/exceptions.html#RuntimeError "RuntimeError") to await on a coroutine more than once.
`coroutine.``send`(*value*)[¶](#coroutine.send "Permalink to this definition")
: Starts or resumes execution of the coroutine. If *value* is `None`,
this is equivalen... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e95e257d-6550-5c9b-8a52-39fd59863ded | docs | python | python_datamodel | 38 | 130 | : Raises the specified exception in the coroutine. This method delegates
to the [`throw()`](expressions.html#generator.throw "generator.throw") method of the iterator that caused
the coroutine to suspend, if it has such a method. Otherwise,
the exception is raised at the suspension point. The result
(... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6026787d-4abd-55aa-8823-a821384d1deb | docs | python | python_datamodel | 38 | 131 | has such a method. Then it raises [`GeneratorExit`](../library/exceptions.html#GeneratorExit "GeneratorExit") at the
suspension point, causing the coroutine to immediately clean itself up.
Finally, the coroutine is marked as having finished executing, even if
it was never started.
Coroutine objects... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
2018d37e-3530-5478-9fa2-4f2d18cf6bb6 | docs | python | python_datamodel | 38 | 132 | raise a [`StopAsyncIteration`](../library/exceptions.html#StopAsyncIteration "StopAsyncIteration") error when the iteration is over.
An example of an asynchronous iterable object:
```None
class Reader:
async def readline(self):
...
def __aiter__(self):
return self
async def __anext__... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
206b94cd-69c2-5eeb-a251-3e9f44edf15e | docs | python | python_datamodel | 38 | 133 | ### 3.4.4. Asynchronous Context Managers[¶](#asynchronous-context-managers "Permalink to this headline")
An *asynchronous context manager* is a *context manager* that is able to
suspend execution in its `__aenter__` and `__aexit__` methods.
Asynchronous context managers can be used in an [`async with`](compound_stmts... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8c050ff9-67dc-529e-bbe2-2c7ee20eef6c | docs | python | python_datamodel | 38 | 134 | async def __aexit__(self, exc_type, exc, tb):
await log('exiting context')
```
New in version 3.5.
Footnotes
[1](#id1)
: It *is* possible in some cases to change an object’s type, under certain
controlled conditions. It generally isn’t a good idea though, since it can
lead to some very strange ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a5de2c2a-38e2-5e5a-862c-8d2b710dd8ed | docs | python | python_datamodel | 38 | 135 | `None` if you want to force fallback to the right operand’s reflected
method—that will instead have the opposite effect of explicitly
*blocking* such fallback.
[4](#id6)
: For operands of the same type, it is assumed that if the non-reflected
method – such as [`__add__()`](#object.__add__ "object.__a... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
12870eb4-8f24-511e-9aeb-bd9fcadb7f1a | docs | python | python_datamodel | 39 | 0 | # 3.Data model¶
Source: https://docs.python.org/3.9/reference/datamodel.html
Version: 3.9
## 3.1. Objects, values and types[¶](#objects-values-and-types "Permalink to this headline")
*Objects* are Python’s abstraction for data. All data in a Python program
is represented by objects or by relations between objects. (... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
67ad8cb3-30d6-56db-86cd-5a7ea577a1c7 | docs | python | python_datamodel | 39 | 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](#id8)
The *value* of some objects can change. Objects w... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
2e81a603-06a2-56e5-9a87-3006cb7bb946 | docs | python | python_datamodel | 39 | 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_39.md |
a8950442-f6bc-5b88-bbbb-0613ce4a947d | docs | python | python_datamodel | 39 | 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_39.md |
47a155ed-d20c-5a4a-987f-3db11326992f | docs | python | python_datamodel | 39 | 4 | Examples of containers are tuples, lists and dictionaries. The references are
part of a container’s value. In most cases, when we talk about the value of a
container, we imply the values, not the identities of the contained objects;
however, when we talk about the mutability of a container, only the identities
of the i... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
29a48fcf-2f0f-5a3b-b5c7-f566f468f9b5 | docs | python | python_datamodel | 39 | 5 | []`, `c` and `d` are guaranteed to refer to two different, unique, newly
created empty lists. (Note that `c = d = []` assigns the same object to both
`c` and `d`.)
## 3.2. The standard type hierarchy[¶](#the-standard-type-hierarchy "Permalink to this headline")
Below is a list of the types that are built into Python.... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0b144ad3-92eb-53a8-8a90-96d4b6dc3d95 | docs | python | python_datamodel | 39 | 6 | object is accessed through the built-in name `None`. It is used to signify the
absence of a value in many situations, e.g., it is returned from functions that
don’t explicitly return anything. Its truth value is false.
NotImplemented
: This type has a single value. There is a single object with this valu... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
8acef8de-a8f5-5301-89c1-9417127a685c | docs | python | python_datamodel | 39 | 7 | it currently evaluates as true, it will emit a [`DeprecationWarning`](../library/exceptions.html#DeprecationWarning "DeprecationWarning").
It will raise a [`TypeError`](../library/exceptions.html#TypeError "TypeError") in a future version of Python.
Ellipsis
: This type has a single value. There is a single ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
4a94222b-fbb8-5a29-93f1-b098365b5c00 | docs | python | python_datamodel | 39 | 8 | [`__repr__()`](#object.__repr__ "object.__repr__") and [`__str__()`](#object.__str__ "object.__str__"), have the following
properties:
* They are valid numeric literals which, when passed to their
class constructor, produce an object having the value of the
original numeric.
* The represent... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
4adc0387-b2c4-5132-98f1-348dfd5efb27 | docs | python | python_datamodel | 39 | 9 | : These represent numbers in an unlimited range, subject to available (virtual)
memory only. For the purpose of shift and mask operations, a binary
representation is assumed, and negative numbers are represented in a variant of
2’s complement which gives the illusion of an ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
85836afc-552b-568f-be62-b39b458252f5 | docs | python | python_datamodel | 39 | 10 | interpretation of shift and mask operations involving negative integers.
[`numbers.Real`](../library/numbers.html#numbers.Real "numbers.Real") ([`float`](../library/functions.html#float "float"))
: These represent machine-level double precision floating point numbers. You are
at the mercy of ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
16ac2133-d97f-5424-93e5-3d4e70e861c5 | docs | python | python_datamodel | 39 | 11 | floating point numbers. The same caveats apply as for floating point numbers.
The real and imaginary parts of a complex number `z` can be retrieved through
the read-only attributes `z.real` and `z.imag`.
Sequences
: These represent finite ordered sets indexed by non-negative numbers. The
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
114870e5-5ce0-585a-8951-681438c2de22 | docs | python | python_datamodel | 39 | 12 | `>=` `0` and *i* `<=` *x* `<` *j*.
Sequences are distinguished according to their mutability:
Immutable sequences
: An object of an immutable sequence type cannot change once it is created. (If
the object contains references to other objects, these other objects may be
mutable and ma... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
1f102407-37e4-5f73-b8ea-cf6dd333da6a | docs | python | python_datamodel | 39 | 13 | range `0 - 10FFFF`; [`chr()`](../library/functions.html#chr "chr") converts an integer in the range
`0 - 10FFFF` to the corresponding length `1` string object.
[`str.encode()`](../library/stdtypes.html#str.encode "str.encode") can be used to convert a [`str`](../library/stdtypes.html... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
21b91f67-a7db-5ef9-8d8d-34bfcf88baa4 | docs | python | python_datamodel | 39 | 14 | tuple can be formed by an empty pair of parentheses.
Bytes
: A bytes object is an immutable array. The items are 8-bit bytes,
represented by integers in the range 0 <= x < 256. Bytes literals
(like `b'abc'`) and the built-in [`bytes()`](../library/stdtypes.html#byt... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
574a3aea-99ae-5208-9249-673827356b51 | docs | python | python_datamodel | 39 | 15 | that there are no special cases needed to form lists of length 0 or 1.)
Byte Arrays
: A bytearray object is a mutable array. They are created by the built-in
[`bytearray()`](../library/stdtypes.html#bytearray "bytearray") constructor. Aside from being mutable
(and ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
8e3b255f-0171-5ebe-84c6-8c8ca6bc3ba7 | docs | python | python_datamodel | 39 | 16 | the built-in function [`len()`](../library/functions.html#len "len") returns the number of items in a set. Common
uses for sets are fast membership testing, removing duplicates from a sequence,
and computing mathematical operations such as intersection, union, difference,
and symmetric difference.
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
df35fb82-c34c-5dc1-8933-e29a7d5c89f9 | docs | python | python_datamodel | 39 | 17 | [`frozenset()`](../library/stdtypes.html#frozenset "frozenset") constructor. As a frozenset is immutable and
[hashable](../glossary.html#term-hashable), it can be used again as an element of another set, or as
a dictionary key.
Mappings
: These represent finite sets of objects indexed by arbi... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
6da11df7-c961-56d3-8eaa-32dd2d1cb41e | docs | python | python_datamodel | 39 | 18 | object identity, the reason being that the efficient implementation of
dictionaries requires a key’s hash value to remain constant. Numeric types used
for keys obey the normal rules for numeric comparison: if two numbers compare
equal (e.g., `1` and `1.0`) then they can be used interchan... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
bbcb8eda-cef9-5bb6-85f4-e724a8d769e3 | docs | python | python_datamodel | 39 | 19 | The extension modules [`dbm.ndbm`](../library/dbm.html#module-dbm.ndbm "dbm.ndbm: The standard \"database\" interface, based on ndbm. (Unix)") and [`dbm.gnu`](../library/dbm.html#module-dbm.gnu "dbm.gnu: GNU's reinterpretation of dbm. (Unix)") provide
additional examples of mapping types, as does the [`... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
8b324131-2a39-5ca0-8282-a2fc18191655 | docs | python | python_datamodel | 39 | 20 | section [Function definitions](compound_stmts.html#function)). It should be called with an argument list
containing the same number of items as the function’s formal parameter
list.
Special attributes:
| Attribute | Meaning | |
| --- | --- | --- |
| `__doc__` |... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9e01dd60-377d-5392-9131-44883946a725 | docs | python | python_datamodel | 39 | 21 | | `__defaults__` | A tuple containing default argument values for those arguments that have defaults, or `None` if no arguments have a default value. | Writable |
| `__code__` | The code object representing the compiled function body. | Writable |
| `__globals__` | A reference to the dictionary ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
64a304e3-70d8-5504-9f8a-b245e7bb8e93 | docs | python | python_datamodel | 39 | 22 | | `__kwdefaults__` | A dict containing defaults for keyword-only parameters. | Writable |
Most of the attributes labelled “Writable” check the type of the assigned value.
Function objects also support getting and setting arbitrary attributes, which
can be used, for example, to attach m... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
c9abdce9-99be-5fb9-ab99-f7285077ffe2 | docs | python | python_datamodel | 39 | 23 | [`cell`](../library/types.html#types.CellType "types.CellType") type can be accessed in the [`types`](../library/types.html#module-types "types: Names for built-in types.")
module.
Instance methods
: An instance method object combines a class, a class instance and any
callable object ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
d1dab8ca-f7e8-5b45-93ec-173c0166dbe0 | docs | python | python_datamodel | 39 | 24 | class (perhaps via an instance of that class), if that attribute is a
user-defined function object or a class method object.
When an instance method object is created by retrieving a user-defined
function object from a class via one of its instances, its
`__self__` attribute is ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
4b38e3c2-4d58-5419-bd78-3f802343f218 | docs | python | python_datamodel | 39 | 25 | `f()`, and `x` is an instance of `C`, calling `x.f(1)` is
equivalent to calling `C.f(x, 1)`.
When an instance method object is derived from a class method object, the
“class instance” stored in `__self__` will actually be the class
itself, so that calling either `x.f(1)` or `C.f... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9bca1f46-ca3a-5c54-a39d-f116a4d15fa8 | docs | python | python_datamodel | 39 | 26 | methods; this *only* happens when the function is an attribute of the
class.
Generator functions
: A function or method which uses the [`yield`](simple_stmts.html#yield) statement (see section
[The yield statement](simple_stmts.html#yield)) is called a *generator function*. Such a fun... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
6ced245a-28d2-5167-86be-c28969c4beaa | docs | python | python_datamodel | 39 | 27 | : A function or method which is defined using [`async def`](compound_stmts.html#async-def) is called
a *coroutine function*. Such a function, when called, returns a
[coroutine](../glossary.html#term-coroutine) object. It may contain [`await`](expressions.html#await) expressions,
as well as... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
a889c990-a0c1-5bbb-8d06-a8da34ab1ded | docs | python | python_datamodel | 39 | 28 | [`aiterator.__anext__`](#object.__anext__ "object.__anext__") method
will return an [awaitable](../glossary.html#term-awaitable) which when awaited
will execute until it provides a value using the [`yield`](simple_stmts.html#yield)
expression. When the function executes an empty [`return... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
35c528f2-dd3c-5d49-bf9e-ac386167dfc6 | docs | python | python_datamodel | 39 | 29 | determined by the C function. Special read-only attributes:
`__doc__` is the function’s documentation string, or `None` if
unavailable; [`__name__`](../library/stdtypes.html#definition.__name__ "definition.__name__") is the function’s name; `__self__` is
set to `None` (but see the next i... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
186e1a1f-e885-582e-9b80-d199d0ba17bb | docs | python | python_datamodel | 39 | 30 | override [`__new__()`](#object.__new__ "object.__new__"). The arguments of the call are passed to
[`__new__()`](#object.__new__ "object.__new__") and, in the typical case, to [`__init__()`](#object.__init__ "object.__init__") to
initialize the new instance.
Class Instances
: Instances... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
a1c24790-664c-5cee-b01a-4d27341c9183 | docs | python | python_datamodel | 39 | 31 | dictionary object (this is the dictionary referenced by the `__globals__`
attribute of functions defined in the module). Attribute references are
translated to lookups in this dictionary, e.g., `m.x` is equivalent to
`m.__dict__["x"]`. A module object does not contain the code object used
to initial... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
2fe0e265-55e5-5da2-9c12-34c59159088e | docs | python | python_datamodel | 39 | 32 | module was loaded, if it was loaded from a file. The [`__file__`](import.html#__file__ "__file__")
attribute may be missing for certain types of modules, such as C modules
that are statically linked into the interpreter; for extension modules
loaded dynamically from a shared library, it is the pathname ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e8f7a9b6-76d5-52d4-ab25-e99f1e82880b | docs | python | python_datamodel | 39 | 33 | [Class definitions](compound_stmts.html#class)). A class has a namespace implemented by a dictionary object.
Class attribute references are translated to lookups in this dictionary, e.g.,
`C.x` is translated to `C.__dict__["x"]` (although there are a number of
hooks which allow for other means of locati... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
56bb0331-3d52-5c8f-b25c-86942243f3fa | docs | python | python_datamodel | 39 | 34 | `__self__` attribute is `C`. When it would yield a static
method object, it is transformed into the object wrapped by the static method
object. See section [Implementing Descriptors](#descriptors) for another way in which attributes
retrieved from a class may differ from those actually contained in its
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
43a0498c-0695-5e38-b136-22f8cca8779d | docs | python | python_datamodel | 39 | 35 | tuple containing the base classes, in the order of their occurrence in the
base class list; `__doc__` is the class’s documentation string,
or `None` if undefined; `__annotations__` (optional) is a dictionary
containing [variable annotations](../glossary.html#term-variable-annotation) collected during
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
6df0dc23-a589-5e12-8c0c-9545e330dc55 | docs | python | python_datamodel | 39 | 36 | section [Implementing Descriptors](#descriptors) for another way in which attributes of a class
retrieved via its instances may differ from the objects actually stored in
the class’s [`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__"). If no class attribute is found, and the
object’... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
d995e325-9484-5b72-8ea9-91debb59491c | docs | python | python_datamodel | 39 | 37 | Special attributes: [`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__") is the attribute dictionary;
[`__class__`](../library/stdtypes.html#instance.__class__ "instance.__class__") is the instance’s class.
I/O objects (also known as file objects)
: A [file object](../glossary.html#term-f... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
fd78c50f-7722-5de8-89f0-a1e18bf5266b | docs | python | python_datamodel | 39 | 38 | input, output and error streams; they are all open in text mode and
therefore follow the interface defined by the [`io.TextIOBase`](../library/io.html#io.TextIOBase "io.TextIOBase")
abstract class.
Internal types
: A few types used internally by the interpreter are exposed to the user. Their
definiti... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
8d7ed9dc-d5d6-5eb0-b56d-1974caf2326f | docs | python | python_datamodel | 39 | 39 | objects, code objects are immutable and contain no references (directly or
indirectly) to mutable objects.
Special read-only attributes: `co_name` gives the function name;
`co_argcount` is the total number of positional arguments
(including positional-only arguments and argument... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.