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 |
|---|---|---|---|---|---|---|---|
7edaf2cf-4d4b-59e9-a291-9f93217f90c1 | docs | python | python_datamodel | 312 | 104 | class’ [`__dict__`](#object.__dict__ "object.__dict__"). The [`object`](../library/functions.html#object "object") class itself does not
implement any of these protocols.
object.\_\_get\_\_(*self*, *instance*, *owner=None*)[¶](#object.__get__ "Link to this definition")
: Called to get the attribute of the owner clas... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
f5febd2f-7dc8-57a2-bb10-76b740fdd293 | docs | python | python_datamodel | 312 | 105 | however, it is likely that some third-party tools have descriptors
that require both arguments. Python’s own [`__getattribute__()`](#object.__getattribute__ "object.__getattribute__")
implementation always passes in both arguments whether they are required
or not.
object.\_\_set\_\_(*self*, *instance*,... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
e8c70d8f-9b66-5d2b-b3a0-3ecb6933d508 | docs | python | python_datamodel | 312 | 106 | object.\_\_objclass\_\_[¶](#object.__objclass__ "Link to this definition")
: The attribute `__objclass__` is interpreted by the [`inspect`](../library/inspect.html#module-inspect "inspect: Extract information and source code from live objects.") module
as specifying the class where this object was defined (settin... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
3f754167-79cb-5336-a1f0-2a732077895d | docs | python | python_datamodel | 312 | 107 | [`__delete__()`](#object.__delete__ "object.__delete__"). If any of
those methods are defined for an object, it is said to be a descriptor.
The default behavior for attribute access is to get, set, or delete the
attribute from an object’s dictionary. For instance, `a.x` has a lookup chain
starting with `a.__dict__['x'... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
a9a5975b-847e-5899-9ce6-bddb7c82c914 | docs | python | python_datamodel | 312 | 108 | : If binding to an object instance, `a.x` is transformed into the call:
`type(a).__dict__['x'].__get__(a, type(a))`.
Class Binding
: If binding to a class, `A.x` is transformed into the call:
`A.__dict__['x'].__get__(None, A)`.
Super Binding
: A dotted lookup such as `super(A, a).x` searches
`a.__cl... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
3cf62e9f-d0e6-5055-ace7-0b45bb533b6e | docs | python | python_datamodel | 312 | 109 | the descriptor defines `__set__()` and/or `__delete__()`, it is a data
descriptor; if it defines neither, it is a non-data descriptor. Normally, data
descriptors define both `__get__()` and `__set__()`, while non-data
descriptors have just the `__get__()` method. Data descriptors with
`__get__()` and `__set__()` (and/o... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
025dca0b-c24f-5e48-8874-5872977f05bf | docs | python | python_datamodel | 312 | 110 | instances cannot override the behavior of a property.
#### 3.3.2.4. \_\_slots\_\_[¶](#slots "Link to this heading")
*\_\_slots\_\_* allow us to explicitly declare data members (like
properties) and deny the creation of [`__dict__`](#object.__dict__ "object.__dict__") and *\_\_weakref\_\_*
(unless explicitly declared ... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
47616437-94d6-5704-9119-8f5b80f548d0 | docs | python | python_datamodel | 312 | 111 | [`__dict__`](#object.__dict__ "object.__dict__") and
*\_\_weakref\_\_* attribute of the instances will always be accessible.
* Without a [`__dict__`](#object.__dict__ "object.__dict__") variable, instances cannot be assigned new
variables not
listed in the *\_\_slots\_\_* definition. Attempts to assign to an un... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
b79700f5-6e75-5003-9d0c-61c61121a0d4 | docs | python | python_datamodel | 312 | 112 | * *\_\_slots\_\_* are implemented at the class level by creating [descriptors](#descriptors)
for each variable name. As a result, class attributes
cannot be used to set default values for instance variables defined by
*\_\_slots\_\_*; otherwise, the class attribute would overwrite the descriptor
assignment.
* T... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
0a8d0dea-605e-5474-aff2-1cd0e739404e | docs | python | python_datamodel | 312 | 113 | * [`TypeError`](../library/exceptions.html#TypeError "TypeError") will be raised if nonempty *\_\_slots\_\_* are defined for a
class derived from a
[`"variable-length" built-in type`](../c-api/typeobj.html#c.PyTypeObject.tp_itemsize "PyTypeObject.tp_itemsize") such as
[`int`](../library/functions.html#int "int"),... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
a535ec50-0156-527a-89ab-8698f81c1fa6 | docs | python | python_datamodel | 312 | 114 | * [`__class__`](#object.__class__ "object.__class__") assignment works only if both classes have the
same *\_\_slots\_\_*.
* [Multiple inheritance](../tutorial/classes.html#tut-multiple) with multiple slotted parent
classes can be used,
but only one parent is allowed to have attributes created by slots
(the oth... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
ba228a2d-1bc4-58df-bc3d-1fc3094f013a | docs | python | python_datamodel | 312 | 115 | change the behavior of subclasses. This is closely related to class
decorators, but where class decorators only affect the specific class they’re
applied to, `__init_subclass__` solely applies to future subclasses of the
class defining the method.
*classmethod* object.\_\_init\_subclass\_\_(*cls*)[¶](#object.__init_su... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
c96b18da-2294-5ce5-afeb-3d723023c188 | docs | python | python_datamodel | 312 | 116 | cls.default_name = default_name
class AustralianPhilosopher(Philosopher, default_name="Bruce"):
pass
```
The default implementation `object.__init_subclass__` does
nothing, but raises an error if it is called with any arguments.
Note
The metaclass hint `metaclass` is cons... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
040e803e-978b-58d6-b7bb-b5a0962976fd | docs | python | python_datamodel | 312 | 117 | x = C() # Automatically calls: x.__set_name__(A, 'x')
```
If the class variable is assigned after the class is created,
[`__set_name__()`](#object.__set_name__ "object.__set_name__") will not be called automatically.
If needed, [`__set_name__()`](#object.__set_name__ "object.__set_name__") can... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
42300170-12cb-5fca-8da0-2ec610358087 | docs | python | python_datamodel | 312 | 118 | keyword argument in the class definition line, or by inheriting from an
existing class that included such an argument. In the following example,
both `MyClass` and `MySubclass` are instances of `Meta`:
```None
class Meta(type):
pass
class MyClass(metaclass=Meta):
pass
class MySubclass(MyClass):
pass
```
... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
f38b79a9-48ae-50da-9088-23f3ea554616 | docs | python | python_datamodel | 312 | 119 | [`type`](../library/functions.html#type "type"), then an `__mro_entries__()` method is searched on the base.
If an `__mro_entries__()` method is found, the base is substituted with the
result of a call to `__mro_entries__()` when creating the class.
The method is called with the original bases tuple
... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
1e669643-3a2d-5930-ac7c-49738da5de9a | docs | python | python_datamodel | 312 | 120 | : Core support for typing module and generic types.
#### 3.3.3.3. Determining the appropriate metaclass[¶](#determining-the-appropriate-metaclass "Link to this heading")
The appropriate metaclass for a class definition is determined as follows:
* if no bases and no explicit metaclass are given, then [`type()`](../... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
533fc92e-471e-5850-aa42-1f28c35f9486 | docs | python | python_datamodel | 312 | 121 | that criterion, then the class definition will fail with `TypeError`.
#### 3.3.3.4. Preparing the class namespace[¶](#preparing-the-class-namespace "Link to this heading")
Once the appropriate metaclass has been identified, then the class namespace
is prepared. If the metaclass has a `__prepare__` attribute, it is ca... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
b21fe5be-73d3-5b72-b2b7-f11943a851c5 | docs | python | python_datamodel | 312 | 122 | #### 3.3.3.5. Executing the class body[¶](#executing-the-class-body "Link to this heading")
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 (includi... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
819bcbf9-f882-559c-8c05-6831bfe40ddc | docs | python | python_datamodel | 312 | 123 | `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_312.md |
3bf700d7-0cf8-5b37-bbd2-8466a7f279e7 | docs | python | python_datamodel | 312 | 124 | 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 customization steps are
invoked aft... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
ec78dc9b-4aed-5a27-bedf-95569db7f1a6 | docs | python | python_datamodel | 312 | 125 | 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__`](#type.__dict__ "type.__dict__") attribute of the class object.
See also
[*... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
bc67e389-d1e8-505c-828e-134e8766da10 | docs | python | python_datamodel | 312 | 126 | [`isinstance()`](../library/functions.html#isinstance "isinstance") and [`issubclass()`](../library/functions.html#issubclass "issubclass") built-in functions.
In particular, the metaclass [`abc.ABCMeta`](../library/abc.html#abc.ABCMeta "abc.ABCMeta") implements these methods in
order to allow the addition of Abstract... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
67d3f447-746e-5184-8c12-4a2f2b37abf4 | docs | python | python_datamodel | 312 | 127 | 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 with
the lookup of special methods that are called on instances, only in this
case the instance is itself a class.
See also
[**PEP 3119**](https://peps.python.org/p... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
7917fefd-cd12-5b28-9404-57cebfbf7af5 | docs | python | python_datamodel | 312 | 128 | ### 3.3.5. Emulating generic types[¶](#emulating-generic-types "Link to this heading")
When using [type annotations](../glossary.html#term-annotation), it is often useful to
*parameterize* a [generic type](../glossary.html#term-generic-type) using Python’s square-brackets notation.
For example, the annotation `list[in... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
3f524adc-7fbb-5fc9-ba06-6475fcf62243 | docs | python | python_datamodel | 312 | 129 | parameterized at runtime and understood by static type-checkers.
A class can *generally* only be parameterized if it defines the special
class method `__class_getitem__()`.
*classmethod* object.\_\_class\_getitem\_\_(*cls*, *key*)[¶](#object.__class_getitem__ "Link to this definition")
: Return an object repres... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
6b7c806c-a252-59b6-badf-f54f7fe7f9b9 | docs | python | python_datamodel | 312 | 130 | To implement custom generic classes that can be parameterized at runtime and
understood by static type-checkers, users should either inherit from a standard
library class that already implements [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__"), or
inherit from [`typing.Generic`](../library/... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
16f0c7ea-3017-5253-bf4f-54fc78ba5fcb | docs | python | python_datamodel | 312 | 131 | brackets will call the [`__getitem__()`](#object.__getitem__ "object.__getitem__") instance method defined on
the object’s class. However, if the object being subscribed is itself a class,
the class method [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__") may be called instead.
`__class_geti... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
460e3860-3395-5c93-8ee1-8b923b814c45 | docs | python | python_datamodel | 312 | 132 | if hasattr(class_of_obj, '__getitem__'):
return class_of_obj.__getitem__(obj, x)
# Else, if obj is a class and defines __class_getitem__,
# call obj.__class_getitem__(x)
elif isclass(obj) and hasattr(obj, '__class_getitem__'):
return obj.__class_getitem__(x)
# Else, raise an except... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
9f6cc337-81e4-528f-8caa-bbac1e3d5f2a | docs | python | python_datamodel | 312 | 133 | ```None
>>> # list has class "type" as its metaclass, like most classes:
>>> type(list)
<class 'type'>
>>> type(dict) == type(list) == type(tuple) == type(str) == type(bytes)
True
>>> # "list[int]" calls "list.__class_getitem__(int)"
>>> list[int]
list[int]
>>> # list.__class_getitem__ returns a GenericAlias object:
>>... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
0bbdf4f2-5090-59f6-a7e0-ad62bc11d089 | docs | python | python_datamodel | 312 | 134 | >>> # and the result is not a GenericAlias object:
>>> Menu['SPAM']
<Menu.SPAM: 'spam'>
>>> type(Menu['SPAM'])
<enum 'Menu'>
```
See also
[**PEP 560**](https://peps.python.org/pep-0560/) - Core Support for typing module and generic types
: Introducing [`__class_getitem__()`](#object.__class_getitem__ "object.__clas... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
46417a8e-56e4-53e2-9ad0-96bd8375bd61 | docs | python | python_datamodel | 312 | 135 | ### 3.3.7. Emulating container types[¶](#emulating-container-types "Link to this heading")
The following methods can be defined to implement container objects. None of them
are provided by the [`object`](../library/functions.html#object "object") class itself. Containers usually are
[sequences](../glossary.html#term-s... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
ed3974cd-d2fa-5d69-83ca-bf61d66db3b8 | docs | python | python_datamodel | 312 | 136 | `keys()`, `values()`, `items()`, `get()`, `clear()`,
`setdefault()`, `pop()`, `popitem()`, `copy()`, and
`update()` behaving similar to those for Python’s standard [`dictionary`](../library/stdtypes.html#dict "dict")
objects. The [`collections.abc`](../library/collections.abc.html#module-collections.abc "collections.ab... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
89da4834-6c69-5fb4-bb02-ee11616eff05 | docs | python | python_datamodel | 312 | 137 | 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__()`](#object.__iadd__ "object.__iadd__"),
[`__mul__()`](#objec... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
ce7f9a9a-01ca-51ed-a15c-48ef09e004d4 | docs | python | python_datamodel | 312 | 138 | through the container; for mappings, `__iter__()` should iterate
through the object’s keys; for sequences, it should iterate through the values.
object.\_\_len\_\_(*self*)[¶](#object.__len__ "Link to this definition")
: Called to implement the built-in function [`len()`](../library/functions.html#len "len"). Should ... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
4ea18f71-5d4f-5ba1-9002-4a6e03197b3a | docs | python | python_datamodel | 312 | 139 | [`__bool__()`](#object.__bool__ "object.__bool__") method.
object.\_\_length\_hint\_\_(*self*)[¶](#object.__length_hint__ "Link to this definition")
: Called to implement [`operator.length_hint()`](../library/operator.html#operator.length_hint "operator.length_hint"). Should return an estimated
length for th... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
84235937-d78f-5b1c-bea6-7f43d6c975b8 | docs | python | python_datamodel | 312 | 140 | object.\_\_getitem\_\_(*self*, *key*)[¶](#object.__getitem__ "Link to this definition")
: Called to implement evaluation of `self[key]`. For [sequence](../glossary.html#term-sequence) types,
the accepted keys should be integers. Optionally, they may support
[`slice`](../library/functions.html#slice "slice") o... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
dc2ffd64-da71-5cf2-b9ab-f98036929b91 | docs | python | python_datamodel | 312 | 141 | [`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 sequence.
Note
When [subscripting](expressions.html#subscriptions) a *class*, the special
class me... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
f6dbeed8-5c0b-5ed3-be64-1d4f4140d898 | docs | python | python_datamodel | 312 | 142 | for improper *key* values as for the [`__getitem__()`](#object.__getitem__ "object.__getitem__") method.
object.\_\_delitem\_\_(*self*, *key*)[¶](#object.__delitem__ "Link to this definition")
: Called to implement deletion of `self[key]`. Same note as for
[`__getitem__()`](#object.__getitem__ "object.__geti... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
61df38b2-d9d9-549d-ae15-e97ad006f121 | docs | python | python_datamodel | 312 | 143 | : This method is called when an [iterator](../glossary.html#term-iterator) is required for a container.
This method should return a new iterator object that can iterate over all the
objects in the container. For mappings, it should iterate over the keys of
the container.
object.\_\_reversed\_\_(*self*)[¶... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
5381df9a-3940-51af-9202-028f055cecec | docs | python | python_datamodel | 312 | 144 | 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_312.md |
ee4fc61b-f6a6-5d83-8d72-5a1c5b88524f | docs | python | python_datamodel | 312 | 145 | 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_312.md |
dc3f3e74-4641-57d2-b4e1-825c05a9f67a | docs | python | python_datamodel | 312 | 146 | object.\_\_truediv\_\_(*self*, *other*)[¶](#object.__truediv__ "Link to this definition")
object.\_\_floordiv\_\_(*self*, *other*)[¶](#object.__floordiv__ "Link to this definition")
object.\_\_mod\_\_(*self*, *other*)[¶](#object.__mod__ "Link to this definition")
object.\_\_divmod\_\_(*self*, *other*)[¶](#object.__d... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
1f7c668b-1ba7-5e32-bdaf-fca499725d03 | docs | python | python_datamodel | 312 | 147 | (`+`, `-`, `*`, `@`, `/`, `//`, `%`, [`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_312.md |
23b6a1ae-325c-572f-9131-40a459ee1c44 | docs | python | python_datamodel | 312 | 148 | arguments, it should return [`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented").
object.\_\_radd\_\_(*self*, *other*)[¶](#object.__radd__ "Link to this definition")
object.\_\_rsub\_\_(*self*, *other*)[¶](#object.__rsub__ "Link to this definition")
object.\_\_rmul\_\_(*self*, *other*)[¶... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
d27ed92b-2bec-542d-b9e7-850f2ca11fa2 | docs | python | python_datamodel | 312 | 149 | object.\_\_rrshift\_\_(*self*, *other*)[¶](#object.__rrshift__ "Link to this definition")
object.\_\_rand\_\_(*self*, *other*)[¶](#object.__rand__ "Link to this definition")
object.\_\_rxor\_\_(*self*, *other*)[¶](#object.__rxor__ "Link to this definition")
object.\_\_ror\_\_(*self*, *other*)[¶](#object.__ror__ "Lin... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
ae46fbfc-d524-5c28-af7d-4f9b92da7562 | docs | python | python_datamodel | 312 | 150 | `type(y).__rsub__(y, x)` is called if `type(x).__sub__(x, y)` returns
[`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented").
Note that ternary [`pow()`](../library/functions.html#pow "pow") will not try calling [`__rpow__()`](#object.__rpow__ "object.__rpow__") (the
coercion rul... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
d0b9e6d0-3e88-575b-b65b-fabc9c30890d | docs | python | python_datamodel | 312 | 151 | object.\_\_imatmul\_\_(*self*, *other*)[¶](#object.__imatmul__ "Link to this definition")
object.\_\_itruediv\_\_(*self*, *other*)[¶](#object.__itruediv__ "Link to this definition")
object.\_\_ifloordiv\_\_(*self*, *other*)[¶](#object.__ifloordiv__ "Link to this definition")
object.\_\_imod\_\_(*self*, *other*)[¶](#... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
939fdd9e-487d-565d-92a4-c235183ed0d0 | docs | python | python_datamodel | 312 | 152 | (`+=`, `-=`, `*=`, `@=`, `/=`, `//=`, `%=`, `**=`, `<<=`,
`>>=`, `&=`, `^=`, `|=`). 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, or if
that method returns [`N... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
01235f0f-9b15-53f9-a419-529696650fe5 | docs | python | python_datamodel | 312 | 153 | [Why does a\_tuple[i] += [‘item’] raise an exception when the addition works?](../faq/programming.html#faq-augmented-assignment-tuple-error)), but this behavior is in fact
part of the data model.
object.\_\_neg\_\_(*self*)[¶](#object.__neg__ "Link to this definition")
object.\_\_pos\_\_(*self*)[¶](#object.__p... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
522b17d8-d19d-5531-a936-8a3b7ea528b2 | docs | python | python_datamodel | 312 | 154 | [`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__ "Link to this definition")
: Called to implement [`operator.index()`](../library/operator.html#operator.ind... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
541da908-2645-5388-9c5b-bee1fca6683f | docs | python | python_datamodel | 312 | 155 | 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_312.md |
f6837073-24d5-5df7-b360-dff48bbe277e | docs | python | python_datamodel | 312 | 156 | 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_312.md |
61a88046-872e-5889-afc8-aa58dd98b2c5 | docs | python | python_datamodel | 312 | 157 | handles the entry into, and the exit from, the desired runtime context for the
execution of the block of code. Context managers are normally invoked using the
`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... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
78d7c825-ded4-5199-85c6-0cbc6ea3d598 | docs | python | python_datamodel | 312 | 158 | object.\_\_exit\_\_(*self*, *exc\_type*, *exc\_value*, *traceback*)[¶](#object.__exit__ "Link to this definition")
: Exit the runtime context related to this object. The parameters describe the
exception that caused the context to be exited. If the context was exited
without an exception, all three arguments ... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
bbdeb59e-7591-57f3-9840-1c202aa93c5c | docs | python | python_datamodel | 312 | 159 | ### 3.3.10. Customizing positional arguments in class pattern matching[¶](#customizing-positional-arguments-in-class-pattern-matching "Link to this heading")
When using a class name in a pattern, positional arguments in the pattern are not
allowed by default, i.e. `case MyClass(x, y)` is typically invalid without spec... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
d3beeb48-5981-5506-8edf-cf497c3335dc | docs | python | python_datamodel | 312 | 160 | that `case MyClass(x, y)` is equivalent to `case MyClass(left=x, center=y)`. Note
that the number of arguments in the pattern must be smaller than or equal to the number
of elements in *\_\_match\_args\_\_*; if it is larger, the pattern match attempt will raise
a [`TypeError`](../library/exceptions.html#TypeError "Type... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
89614c65-7c0e-5bd7-b73d-919aaae095a9 | docs | python | python_datamodel | 312 | 161 | While buffer types are usually implemented in C, it is also possible to
implement the protocol in Python.
object.\_\_buffer\_\_(*self*, *flags*)[¶](#object.__buffer__ "Link to this definition")
: Called when a buffer is requested from *self* (for example, by the
[`memoryview`](../library/stdtypes.html#memoryview... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
a7816999-ddc3-5bc1-9217-b4d7eaa4fae1 | docs | python | python_datamodel | 312 | 162 | [`memoryview`](../library/stdtypes.html#memoryview "memoryview") object that was previously returned by
[`__buffer__()`](#object.__buffer__ "object.__buffer__"). The method must release any resources associated
with the buffer. This method should return `None`.
Buffer objects that do not need to perform... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
dac8a303-2b05-506e-9e24-9daa2f1878c5 | docs | python | python_datamodel | 312 | 163 | 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_312.md |
f1a299c7-52f3-5887-b8a9-9306293b1183 | docs | python | python_datamodel | 312 | 164 | 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_312.md |
e774d6a7-2706-5fc1-8974-580ebf62bbc3 | docs | python | python_datamodel | 312 | 165 | >>> 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_312.md |
788d50b6-e87c-50a6-91d3-204ee486a9a5 | docs | python | python_datamodel | 312 | 166 | The [generator iterator](../glossary.html#term-generator-iterator) objects returned from generators
decorated with [`types.coroutine()`](../library/types.html#types.coroutine "types.coroutine")
are also awaitable, but they do not implement [`__await__()`](#object.__await__ "object.__await__").
object.\_\_await\_\_(*se... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
36fb3796-fc21-5698-a687-2187b5c56cf8 | docs | python | python_datamodel | 312 | 167 | 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.
Added in version 3.5.
See also
[**PEP 492**](https://peps.python.org/pe... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
3a25437a-9cef-5026-b394-c3e8ab396e76 | docs | python | python_datamodel | 312 | 168 | 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_312.md |
c8fe3654-2133-53c9-a954-fa6b1213c440 | docs | python | python_datamodel | 312 | 169 | 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_312.md |
1fef3799-d79c-5f5b-a821-97b58e64d756 | docs | python | python_datamodel | 312 | 170 | Changed in version 3.12: The second signature (type[, value[, traceback]]) is deprecated and
may be removed in a future version of Python.
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 ... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
af305b40-2818-5955-ab94-a8bebb98b1b4 | docs | python | python_datamodel | 312 | 171 | ### 3.4.3. Asynchronous Iterators[¶](#asynchronous-iterators "Link to this heading")
An *asynchronous iterator* can call asynchronous code in
its `__anext__` method.
Asynchronous iterators can be used in an [`async for`](compound_stmts.html#async-for) statement.
The [`object`](../library/functions.html#object "objec... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
046f18d3-e92a-5a68-b60e-9f4a60b74241 | docs | python | python_datamodel | 312 | 172 | if val == b'':
raise StopAsyncIteration
return val
```
Added in version 3.5.
Changed in version 3.7: Prior to Python 3.7, [`__aiter__()`](#object.__aiter__ "object.__aiter__") could return an *awaitable*
that would resolve to an
[asynchronous iterator](../glossary.html#term-asynchronous-it... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
f36c99c2-276f-5d5a-9a5a-009ce8b41938 | docs | python | python_datamodel | 312 | 173 | object.\_\_aenter\_\_(*self*)[¶](#object.__aenter__ "Link to this definition")
: Semantically similar to [`__enter__()`](#object.__enter__ "object.__enter__"), the only
difference being that it must return an *awaitable*.
object.\_\_aexit\_\_(*self*, *exc\_type*, *exc\_value*, *traceback*)[¶](#object.__aexit__ "... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
81678269-2967-5f5c-b962-0e771b05a424 | docs | python | python_datamodel | 312 | 174 | The [`__hash__()`](#object.__hash__ "object.__hash__"), [`__iter__()`](#object.__iter__ "object.__iter__"),
[`__reversed__()`](#object.__reversed__ "object.__reversed__"), and [`__contains__()`](#object.__contains__ "object.__contains__") methods have
special handling for this; others
will still raise a [`TypeError`](.... | large_dataset/raw/docs/python/datamodel/datamodel_312.md |
691bc7d1-fe98-5f30-92f1-991c65ca726f | docs | python | python_datamodel | 312 | 175 | supported, which is why the reflected method is not called.
| large_dataset/raw/docs/python/datamodel/datamodel_312.md |
cfe2bf8a-6154-56ee-8a60-b6dee339f3b0 | docs | python | python_datamodel | 313 | 0 | # 3.Data model¶
Source: https://docs.python.org/3.13/reference/datamodel.html
Version: 3.13
## 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. Even ... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
0a89c753-1ed1-548a-96c8-3eb004d732ed | docs | python | python_datamodel | 313 | 1 | 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]](#id19)
The *value* of some objects can change. Objects whose value can
change are said to be *mutable*; objects whose value is unc... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
f84eadc7-7291-5fca-ac7b-4687f482dc4b | docs | python | python_datamodel | 313 | 2 | 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 still reachable.
**CPython implementation detail:** CPython currently uses a re... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
2d855f44-8747-56b1-b6fb-74e03fefad32 | docs | python | python_datamodel | 313 | 3 | 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 “external” resources such as open files or
windows. It is understood that these... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
eadd6e76-0a05-5f57-b108-44680be242e0 | docs | python | python_datamodel | 313 | 4 | 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 immediately contained objects are implied. So, if an immutable container
(like a tuple) contains a reference to a mutable object, its value changes if
that m... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
3e80ad35-4348-523a-bcb7-cfc52c92b396 | docs | python | python_datamodel | 313 | 5 | not be relied upon, but is something to be aware of when making use of object
identity tests.
However, after `c = []; d = []`, *c* and *d* are guaranteed to refer to two
different, unique, newly created empty lists. (Note that `e = f = []` assigns
the *same* object to both *e* and *f*.)
## 3.2. The standard type hiera... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
396fc2fe-36f9-5bd9-9ce4-aee0eb6073ab | docs | python | python_datamodel | 313 | 6 | ### 3.2.1. None[¶](#none "Link to this heading")
This type has a single value. There is a single object with this value. This
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. ... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
890ddd96-433d-55f8-b3f2-9e574022b24a | docs | python | python_datamodel | 313 | 7 | for more details.
Changed in version 3.9: Evaluating [`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented") in a boolean context is deprecated. While
it currently evaluates as true, it will emit a [`DeprecationWarning`](../library/exceptions.html#DeprecationWarning "DeprecationWarning").
It will... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
996e84b4-5a7c-5d4a-abe6-8cb3ebb3206c | docs | python | python_datamodel | 313 | 8 | related to mathematical numbers, but subject to the limitations of numerical
representation in computers.
The string representations of the numeric classes, computed by
[`__repr__()`](#object.__repr__ "object.__repr__") and [`__str__()`](#object.__str__ "object.__str__"), have the following
properties:
* They are val... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
f7dfb9ac-a21c-57cd-8292-c2aea788a3aa | docs | python | python_datamodel | 313 | 9 | #### 3.2.4.1. [`numbers.Integral`](../library/numbers.html#numbers.Integral "numbers.Integral")[¶](#numbers-integral "Link to this heading")
These represent elements from the mathematical set of integers (positive and
negative).
Note
The rules for integer representation are intended to give the most meaningful
inter... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
41554436-edc7-5f44-94a2-fdec08b74fec | docs | python | python_datamodel | 313 | 10 | the values `False` and `True` are the only Boolean objects. The Boolean type is a
subtype of the integer type, and Boolean values behave like the values 0 and 1,
respectively, in almost all contexts, the exception being that when converted to
a string, the strings `"False"` or `"True"` are returned, res... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
472b55b4-693b-530e-a2a4-b27b0ddb1550 | docs | python | python_datamodel | 313 | 11 | language with two kinds of floating-point numbers.
#### 3.2.4.3. [`numbers.Complex`](../library/numbers.html#numbers.Complex "numbers.Complex") ([`complex`](../library/functions.html#complex "complex"))[¶](#numbers-complex-complex "Link to this heading")
These represent complex numbers as a pair of machine-level doub... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
04811842-84c0-58e1-8410-7f66493fc88a | docs | python | python_datamodel | 313 | 12 | sequence length. For example, `a[-2]` equals `a[n-2]`, the second to last
item of sequence a with length `n`.
Sequences also support slicing: `a[i:j]` selects all items with index *k* such
that *i* `<=` *k* `<` *j*. When used as an expression, a slice is a
sequence of the same type. The comment above about negative in... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
952afb5e-caee-5a29-990f-b1b3820508e6 | docs | python | python_datamodel | 313 | 13 | Strings
: A string is a sequence of values that represent Unicode code points.
All the code points in the range `U+0000 - U+10FFFF` can be
represented in a string. Python doesn’t have a char type;
instead, every code point in the string is represented as a string
object with length `1`. The built-in f... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
257fdb1b-c91f-5249-a036-3334f92780de | docs | python | python_datamodel | 313 | 14 | : The items of a tuple are arbitrary Python objects. Tuples of two or
more items are formed by comma-separated lists of expressions. A tuple
of one item (a ‘singleton’) can be formed by affixing a comma to an
expression (an expression by itself does not create a tuple, since
parentheses must be usable... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
680e1a6f-7933-55ac-b102-c40f681c13c6 | docs | python | python_datamodel | 313 | 15 | #### 3.2.5.2. Mutable sequences[¶](#mutable-sequences "Link to this heading")
Mutable sequences can be changed after they are created. The subscription and
slicing notations can be used as the target of assignment and [`del`](simple_stmts.html#del)
(delete) statements.
Note
The [`collections`](../library/collections... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
9429c00c-522c-5162-856f-68cff02ea1e8 | docs | python | python_datamodel | 313 | 16 | [`bytearray()`](../library/stdtypes.html#bytearray "bytearray") constructor. Aside from being mutable
(and hence unhashable), byte arrays otherwise provide the same interface
and functionality as immutable [`bytes`](../library/stdtypes.html#bytes "bytes") objects.
### 3.2.6. Set types[¶](#set-types "Link t... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
e2303b20-db67-5c8f-af69-b7f1c28a349e | docs | python | python_datamodel | 313 | 17 | compare equal (e.g., `1` and `1.0`), only one of them can be contained in a
set.
There are currently two intrinsic set types:
Sets
: These represent a mutable set. They are created by the built-in [`set()`](../library/stdtypes.html#set "set")
constructor and can be modified afterwards by several methods, such a... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
eaededf3-ded5-5904-a306-da06189cc929 | docs | python | python_datamodel | 313 | 18 | [`del`](simple_stmts.html#del) statements. The built-in function [`len()`](../library/functions.html#len "len") returns the number
of items in a mapping.
There is currently a single intrinsic mapping type:
#### 3.2.7.1. Dictionaries[¶](#dictionaries "Link to this heading")
These represent finite sets of objects inde... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
d4893918-d3af-54d7-9379-a493a90061b1 | docs | python | python_datamodel | 313 | 19 | Replacing an existing key does not change the order, however removing a key
and re-inserting it will add it to the end instead of keeping its old place.
Dictionaries are mutable; they can be created by the `{}` notation (see
section [Dictionary displays](expressions.html#dict)).
The extension modules [`dbm.ndbm`](../... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
55fd9ee2-7743-5e3a-9f76-7b110cd8577d | docs | python | python_datamodel | 313 | 20 | ### 3.2.8. Callable types[¶](#callable-types "Link to this heading")
These are the types to which the function call operation (see section
[Calls](expressions.html#calls)) can be applied:
#### 3.2.8.1. User-defined functions[¶](#user-defined-functions "Link to this heading")
A user-defined function object is created... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
72f3eca7-2868-50d8-b32b-5d565b21d423 | docs | python | python_datamodel | 313 | 21 | | function.\_\_globals\_\_[¶](#function.__globals__ "Link to this definition") | A reference to the [`dictionary`](../library/stdtypes.html#dict "dict") that holds the function’s [global variables](executionmodel.html#naming) – the global namespace of the module in which the function was defined. |
| function.\_\_closu... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
f5faaccc-9662-57af-91b5-3ff7f003304a | docs | python | python_datamodel | 313 | 22 | | function.\_\_doc\_\_[¶](#function.__doc__ "Link to this definition") | The function’s documentation string, or `None` if unavailable. |
| function.\_\_name\_\_[¶](#function.__name__ "Link to this definition") | The function’s name. See also: [`__name__ attributes`](../library/stdtypes.html#definition.__name__ "defini... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
1f875c1f-853f-558e-a2ed-39e361698bca | docs | python | python_datamodel | 313 | 23 | | function.\_\_defaults\_\_[¶](#function.__defaults__ "Link to this definition") | A [`tuple`](../library/stdtypes.html#tuple "tuple") containing default [parameter](../glossary.html#term-parameter) values for those parameters that have defaults, or `None` if no parameters have a default value. |
| function.\_\_code\_\... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
572b20ac-fd41-58e8-8d7f-0888a092bf38 | docs | python | python_datamodel | 313 | 24 | | function.\_\_annotations\_\_[¶](#function.__annotations__ "Link to this definition") | A [`dictionary`](../library/stdtypes.html#dict "dict") containing annotations of [parameters](../glossary.html#term-parameter). The keys of the dictionary are the parameter names, and `'return'` for the return annotation, if provid... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
0c53611f-b7b8-5489-aa85-07ce4fe5249e | docs | python | python_datamodel | 313 | 25 | can be used, for example, to attach metadata to functions. Regular attribute
dot-notation is used to get and set such attributes.
**CPython implementation detail:** CPython’s current implementation only supports function attributes
on user-defined functions. Function attributes on
[built-in functions](#builtin-functio... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
775c0dc0-6d8d-5b06-95c9-6a707d3263d5 | docs | python | python_datamodel | 313 | 26 | | method.\_\_func\_\_[¶](#method.__func__ "Link to this definition") | Refers to the original [function object](#user-defined-funcs) |
| method.\_\_doc\_\_[¶](#method.__doc__ "Link to this definition") | The method’s documentation (same as [`method.__func__.__doc__`](#function.__doc__ "function.__doc__")). A [`string`]... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
5e3560e7-ef5e-50a8-a2b0-dfa574ef1d71 | docs | python | python_datamodel | 313 | 27 | class (perhaps via an instance of that class), if that attribute is a
user-defined [function object](#user-defined-funcs) or a
[`classmethod`](../library/functions.html#classmethod "classmethod") object.
When an instance method object is created by retrieving a user-defined
[function object](#user-defined-funcs) from ... | large_dataset/raw/docs/python/datamodel/datamodel_313.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.