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 |
|---|---|---|---|---|---|---|---|
e04b42e3-a88b-5a53-bbd5-3e1ca82f7ede | docs | python | python_datamodel | 39 | 40 | representing the sequence of bytecode instructions; `co_consts` is
a tuple containing the literals used by the bytecode; `co_names` is
a tuple containing the names used by the bytecode; `co_filename` is
the filename from which the code was compiled; `co_firstlineno` is
the first ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
63937c0a-83bc-5947-90cb-485bfc937e60 | docs | python | python_datamodel | 39 | 41 | Future feature declarations (`from __future__ import division`) also use bits
in `co_flags` to indicate whether a code object was compiled with a
particular feature enabled: bit `0x2000` is set if the function was compiled
with future division enabled; bits `0x10` and `0x1000` were used ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
7566227f-be40-5fd1-beb7-38a5896d28f9 | docs | python | python_datamodel | 39 | 42 | is the dictionary used to look up local variables; `f_globals` is used for
global variables; `f_builtins` is used for built-in (intrinsic) names;
`f_lasti` gives the precise instruction (this is an index into the
bytecode string of the code object).
Accessing `f_code` raises an ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0ab0983d-593a-531c-b6a5-25f5953e99c8 | docs | python | python_datamodel | 39 | 43 | undefined interpreter behaviour if exceptions raised by the trace
function escape to the function being traced.
`f_lineno` is the current line number of the frame — writing to this
from within a trace function jumps to the given line (only for the bottom-most
frame). A debugger ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
da2a48e2-e512-5556-a88a-17896ed207e6 | docs | python | python_datamodel | 39 | 44 | New in version 3.4.
Traceback objects
: Traceback objects represent a stack trace of an exception. A traceback object
is implicitly created when an exception occurs, and may also be explicitly
created by calling [`types.TracebackType`](../library/types.html#types.TracebackType "ty... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0b1726d5-2d1e-58a7-a76f-e2a806b98d07 | docs | python | python_datamodel | 39 | 45 | stream; if the interpreter is interactive, it is also made available to the user
as `sys.last_traceback`.
For explicitly created tracebacks, it is up to the creator of the traceback
to determine how the `tb_next` attributes should be linked to form a
full stack trace.
S... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
353da229-110c-52f4-b948-18df0ebcf804 | docs | python | python_datamodel | 39 | 46 | Special writable attribute: `tb_next` is the next level in the stack
trace (towards the frame where the exception occurred), or `None` if
there is no next level.
Changed in version 3.7: Traceback objects can now be explicitly instantiated from Python code,
and the `tb_next` attr... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
564e981e-55b2-50a5-a832-2503c3a809af | docs | python | python_datamodel | 39 | 47 | : This method takes a single integer argument *length* and computes
information about the slice that the slice object would describe if
applied to a sequence of *length* items. It returns a tuple of three
integers; respectively these are the *start* and *stop* indices and t... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
af6a7d32-8658-582e-821e-e3f09689f6ce | docs | python | python_datamodel | 39 | 48 | objects they wrap usually are. Static method objects are created by the built-in
[`staticmethod()`](../library/functions.html#staticmethod "staticmethod") constructor.
Class method objects
: A class method object, like a static method object, is a wrapper around another
object that al... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
a74a23da-1066-5b8e-a346-244796ac97b2 | docs | python | python_datamodel | 39 | 49 | allowing classes to define their own behavior with respect to language
operators. For instance, if a class defines a method named
[`__getitem__()`](#object.__getitem__ "object.__getitem__"),
and `x` is an instance of this class, then `x[i]` is roughly equivalent
to `type(x).__getitem__(x, i)`. Except where mentioned, a... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
2061662f-fb8c-56b2-9e32-432198d0f135 | docs | python | python_datamodel | 39 | 50 | falling back to [`__getitem__()`](#object.__getitem__ "object.__getitem__")). [2](#id9)
When implementing a class that emulates any built-in type, it is important that
the emulation only be implemented to the degree that it makes sense for the
object being modelled. For example, some sequences may work well with retri... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
ca422ff6-a650-57be-ba73-67c98f1ccf35 | docs | python | python_datamodel | 39 | 51 | class). The return value of [`__new__()`](#object.__new__ "object.__new__") should be the new object instance
(usually an instance of *cls*).
Typical implementations create a new instance of the class by invoking the
superclass’s [`__new__()`](#object.__new__ "object.__new__") method using `super().__n... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0aefe426-7865-5747-99a9-a5bcf74ccb9e | docs | python | python_datamodel | 39 | 52 | [`__init__()`](#object.__init__ "object.__init__") method will not be invoked.
[`__new__()`](#object.__new__ "object.__new__") is intended mainly to allow subclasses of immutable types (like
int, str, or tuple) to customize instance creation. It is also commonly
overridden in custom metaclasses in orde... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
a36dab5e-65fb-5b4b-9639-7c56b1c7015a | docs | python | python_datamodel | 39 | 53 | Because [`__new__()`](#object.__new__ "object.__new__") and [`__init__()`](#object.__init__ "object.__init__") work together in constructing
objects ([`__new__()`](#object.__new__ "object.__new__") to create it, and [`__init__()`](#object.__init__ "object.__init__") to customize it),
no non-`None` value may... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
647eb1ee-3370-5a52-b36e-bdeec904ca19 | docs | python | python_datamodel | 39 | 54 | It is possible (though not recommended!) for the [`__del__()`](#object.__del__ "object.__del__") method
to postpone destruction of the instance by creating a new reference to
it. This is called object *resurrection*. It is implementation-dependent
whether [`__del__()`](#object.__del__ "object.__del__") ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
7e374510-aef1-5105-97b4-039f3e21d306 | docs | python | python_datamodel | 39 | 55 | of an object from going to zero. In this case, the cycle will be
later detected and deleted by the [cyclic garbage collector](../glossary.html#term-garbage-collection). A common cause of reference cycles is when
an exception has been caught in a local variable. The frame’s
locals then reference the exce... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
1f7b6cf5-c33a-541d-b2df-bc2da29b2565 | docs | python | python_datamodel | 39 | 56 | including from any arbitrary thread. If [`__del__()`](#object.__del__ "object.__del__") needs to take
a lock or invoke any other blocking resource, it may deadlock as
the resource may already be taken by the code that gets interrupted
to execute [`__del__()`](#object.__del__ "object.__del__").
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
85408637-432f-54c1-8998-3213a0235d39 | docs | python | python_datamodel | 39 | 57 | : Called by the [`repr()`](../library/functions.html#repr "repr") built-in function to compute the “official” string
representation of an object. If at all possible, this should look like a
valid Python expression that could be used to recreate an object with the
same value (given an appropriate environme... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
4c4a83bc-41ab-5842-9776-2571095cd20f | docs | python | python_datamodel | 39 | 58 | : Called by [`str(object)`](../library/stdtypes.html#str "str") and the built-in functions
[`format()`](../library/functions.html#format "format") and [`print()`](../library/functions.html#print "print") to compute the “informal” or nicely
printable string representation of an object. The return value must be... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
681954d7-50f9-55d5-a472-4760ad83599e | docs | python | python_datamodel | 39 | 59 | of an object. This should return a [`bytes`](../library/stdtypes.html#bytes "bytes") object.
`object.``__format__`(*self*, *format\_spec*)[¶](#object.__format__ "Permalink to this definition")
: Called by the [`format()`](../library/functions.html#format "format") built-in function,
and by extension, evaluat... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
d693d820-734c-58d2-85be-33e586148af6 | docs | python | python_datamodel | 39 | 60 | See [Format Specification Mini-Language](../library/string.html#formatspec) for a description of the standard formatting syntax.
The return value must be a string object.
Changed in version 3.4: The \_\_format\_\_ method of `object` itself raises a [`TypeError`](../library/exceptions.html#TypeError "TypeE... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9434af9a-1a7d-5f57-acf9-9465171f338f | docs | python | python_datamodel | 39 | 61 | : These are the so-called “rich comparison” methods. The correspondence between
operator symbols and method names is as follows: `x<y` calls `x.__lt__(y)`,
`x<=y` calls `x.__le__(y)`, `x==y` calls `x.__eq__(y)`, `x!=y` calls
`x.__ne__(y)`, `x>y` calls `x.__gt__(y)`, and `x>=y` calls
`x.__ge__(y)`.
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
d84d0611-733a-5259-af7d-2e56efc2bc3e | docs | python | python_datamodel | 39 | 62 | `True if x is y else NotImplemented`. For [`__ne__()`](#object.__ne__ "object.__ne__"), by default it
delegates to [`__eq__()`](#object.__eq__ "object.__eq__") and inverts the result unless it is
`NotImplemented`. There are no other implied relationships among the
comparison operators or default impleme... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
df88e0aa-4461-5f96-b441-e10a809e39de | docs | python | python_datamodel | 39 | 63 | rather, [`__lt__()`](#object.__lt__ "object.__lt__") and [`__gt__()`](#object.__gt__ "object.__gt__") are each other’s reflection,
[`__le__()`](#object.__le__ "object.__le__") and [`__ge__()`](#object.__ge__ "object.__ge__") are each other’s reflection, and
[`__eq__()`](#object.__eq__ "object.__eq__") and [... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
40fb9d88-dccb-54d1-875b-8fcd698b09ce | docs | python | python_datamodel | 39 | 64 | [`dict`](../library/stdtypes.html#dict "dict"). The `__hash__()` method should return an integer. The only required
property is that objects which compare equal have the same hash value; it is
advised to mix together the hash values of the components of the object that
also play a part in comparison of ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
053dbbfc-e956-51b8-b01f-3610ccd0577b | docs | python | python_datamodel | 39 | 65 | to do this is with
`python -c "import sys; print(sys.hash_info.width)"`.
If a class does not define an [`__eq__()`](#object.__eq__ "object.__eq__") method it should not define a
[`__hash__()`](#object.__hash__ "object.__hash__") operation either; if it defines [`__eq__()`](#object.__eq__ "object.__eq__... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
cd66a6c8-af6c-5c68-a753-dc6b9cbc7665 | docs | python | python_datamodel | 39 | 66 | by default; with them, all objects compare unequal (except with themselves)
and `x.__hash__()` returns an appropriate value such that `x == y`
implies both that `x is y` and `hash(x) == hash(y)`.
A class that overrides [`__eq__()`](#object.__eq__ "object.__eq__") and does not define [`__hash__()`](#obj... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
37193e32-6835-5669-bf43-3a098778415e | docs | python | python_datamodel | 39 | 67 | of [`__hash__()`](#object.__hash__ "object.__hash__") from a parent class, the interpreter must be told this
explicitly by setting `__hash__ = <ParentClass>.__hash__`.
If a class that does not override [`__eq__()`](#object.__eq__ "object.__eq__") wishes to suppress hash
support, it should include `__ha... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
647978b4-dce1-5eb8-a75d-52dfd234f87d | docs | python | python_datamodel | 39 | 68 | This is intended to provide protection against a denial-of-service caused
by carefully-chosen inputs that exploit the worst case performance of a
dict insertion, O(n2) complexity. See
<http://www.ocert.org/advisories/ocert-2011-003.html> for details.
Changing hash values affects the iteration order... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9f174a2c-ce6a-5e6b-a20e-42e6e499976b | docs | python | python_datamodel | 39 | 69 | [`__len__()`](#object.__len__ "object.__len__") nor [`__bool__()`](#object.__bool__ "object.__bool__"), all its instances are considered
true.
### 3.3.2. Customizing attribute access[¶](#customizing-attribute-access "Permalink to this headline")
The following methods can be defined to customize the meaning of... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
6990e1f4-e83f-56e0-a026-e8c4a89799d5 | docs | python | python_datamodel | 39 | 70 | [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError")). This method should either return the (computed)
attribute value or raise an [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError") exception.
Note that if the attribute is found through the normal mecha... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
1077ec2c-f5f1-5c31-ac52-4cd3e7ae7f65 | docs | python | python_datamodel | 39 | 71 | [`__getattribute__()`](#object.__getattribute__ "object.__getattribute__") method below for a way to actually get total control
over attribute access.
`object.``__getattribute__`(*self*, *name*)[¶](#object.__getattribute__ "Permalink to this definition")
: Called unconditionally to implement attribute access... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
3f99f709-7b45-52db-b343-63ef02787d9b | docs | python | python_datamodel | 39 | 72 | method with the same name to access any attributes it needs, for example,
`object.__getattribute__(self, name)`.
Note
This method may still be bypassed when looking up special methods as the
result of implicit invocation via language syntax or built-in functions.
See [Special method lookup](#s... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
fcc324d0-8565-5913-b603-9b47b30a8e52 | docs | python | python_datamodel | 39 | 73 | `object.__setattr__(self, name, value)`.
For certain sensitive attribute assignments, raises an
[auditing event](../library/sys.html#auditing) `object.__setattr__` with arguments
`obj`, `name`, `value`.
`object.``__delattr__`(*self*, *name*)[¶](#object.__delattr__ "Permalink to this definition")
: L... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
14f741db-f7ed-5e61-8c20-70bd9d8f1e36 | docs | python | python_datamodel | 39 | 74 | #### 3.3.2.1. Customizing module attribute access[¶](#customizing-module-attribute-access "Permalink to this headline")
Special names `__getattr__` and `__dir__` can be also used to customize
access to module attributes. The `__getattr__` function at the module level
should accept one argument which is the name of an ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
f9cd9cb2-3d79-5f37-9cb3-740ff6b89823 | docs | python | python_datamodel | 39 | 75 | function overrides the standard [`dir()`](../library/functions.html#dir "dir") search on a module.
For a more fine grained customization of the module behavior (setting
attributes, properties, etc.), one can set the `__class__` attribute of
a module object to a subclass of [`types.ModuleType`](../library/types.html#ty... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9506c60b-51d7-59d4-b435-9679c14f6ba7 | docs | python | python_datamodel | 39 | 76 | Changed in version 3.5: `__class__` module attribute is now writable.
New in version 3.7: `__getattr__` and `__dir__` module attributes.
See also
[**PEP 562**](https://www.python.org/dev/peps/pep-0562) - Module \_\_getattr\_\_ and \_\_dir\_\_
: Describes the `__getattr__` and `__dir__` functions on modules.
#### ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
c2c66d46-9a0c-572f-97dd-2c84b2e8df34 | docs | python | python_datamodel | 39 | 77 | : Called to get the attribute of the owner class (class attribute access) or
of an instance of that class (instance attribute access). The optional
*owner* argument is the owner class, while *instance* is the instance that
the attribute was accessed through, or `None` when the attribute is
accessed th... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
ac06ae14-a661-5b01-8a2a-922ecb2d9687 | docs | python | python_datamodel | 39 | 78 | `object.``__set__`(*self*, *instance*, *value*)[¶](#object.__set__ "Permalink to this definition")
: 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.__... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
2f1c85fa-46c3-5782-a228-5afd7f9f72c9 | docs | python | python_datamodel | 39 | 79 | [`type`](../library/functions.html#type "type") constructor, so it will need to be called explicitly with
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... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
32c67ced-2afb-560e-9e9f-b09bdd1cf5a6 | docs | python | python_datamodel | 39 | 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_39.md |
414ae4d7-8c41-5f08-82b7-1671a6239da2 | docs | python | python_datamodel | 39 | 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_39.md |
8ea4f113-30c1-59cc-a5cc-5872da4b271f | docs | python | python_datamodel | 39 | 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_39.md |
a09999f3-bbbf-504c-94f9-5faf33779717 | docs | python | python_datamodel | 39 | 83 | [`__get__()`](#object.__get__ "object.__get__") and [`__set__()`](#object.__set__ "object.__set__") (and/or [`__delete__()`](#object.__delete__ "object.__delete__")) defined always override a redefinition in an
instance dictionary. In contrast, non-data descriptors can be overridden by
instances.
Python methods (inclu... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
17573ca3-8282-5775-a47a-a1224b343543 | docs | python | python_datamodel | 39 | 84 | #### 3.3.2.4. \_\_slots\_\_[¶](#slots "Permalink to this headline")
*\_\_slots\_\_* allow us to explicitly declare data members (like
properties) and deny the creation of [`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__") and *\_\_weakref\_\_*
(unless explicitly declared in *\_\_slots\_\_* or ava... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
c7dc49f1-f0fd-519d-a8a5-114965539e96 | docs | python | python_datamodel | 39 | 85 | ##### 3.3.2.4.1. Notes on using *\_\_slots\_\_*[¶](#notes-on-using-slots "Permalink to this headline")
* When inheriting from a class without *\_\_slots\_\_*, the
[`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__") and
*\_\_weakref\_\_* attribute of the instances will always be accessible.
* W... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e532590e-af13-55d1-b8cb-ab45528da31f | docs | python | python_datamodel | 39 | 86 | *\_\_slots\_\_* do not support [`weak references`](../library/weakref.html#module-weakref "weakref: Support for weak references and weak dictionaries.") to its instances.
If weak reference
support is needed, then add `'__weakref__'` to the sequence of strings in the
*\_\_slots\_\_* declaration.
* *\_\_slots\_\_... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e557b65a-d699-55f9-9610-526bfd7eb5d2 | docs | python | python_datamodel | 39 | 87 | * If a class defines a slot also defined in a base class, the instance variable
defined by the base class slot is inaccessible (except by retrieving its
descriptor directly from the base class). This renders the meaning of the
program undefined. In the future, a check may be added to prevent this.
* Nonempty *\_\... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
11de5d20-f230-5de7-b819-c6112c28305b | docs | python | python_datamodel | 39 | 88 | [`inspect.getdoc()`](../library/inspect.html#inspect.getdoc "inspect.getdoc") and displayed in the output of [`help()`](../library/functions.html#help "help").
* [`__class__`](../library/stdtypes.html#instance.__class__ "instance.__class__") assignment works only if both classes have the
same *\_\_slots\_\_*.
* [Mu... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
a413f4a2-0b4a-5a3e-bda1-e1ecb9f644d0 | docs | python | python_datamodel | 39 | 89 | ### 3.3.3. Customizing class creation[¶](#customizing-class-creation "Permalink to this headline")
Whenever a class inherits from another class, [`__init_subclass__()`](#object.__init_subclass__ "object.__init_subclass__") is
called on the parent class. This way, it is possible to write classes which
change the behavi... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
1b750b35-c8df-535b-8cce-b6633a4fba33 | docs | python | python_datamodel | 39 | 90 | other classes using `__init_subclass__`, one should take out the
needed keyword arguments and pass the others over to the base
class, as in:
```None
class Philosopher:
def __init_subclass__(cls, /, default_name, **kwargs):
super().__init_subclass__(**kwargs)
cls.defa... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
f9785994-88af-5378-89f0-dca13af6d8a5 | docs | python | python_datamodel | 39 | 91 | #### 3.3.3.1. Metaclasses[¶](#metaclasses "Permalink to this headline")
By default, classes are constructed using [`type()`](../library/functions.html#type "type"). The class body is
executed in a new namespace and the class name is bound locally to the
result of `type(name, bases, namespace)`.
The class creation pro... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0c7d750b-4ab7-5ee0-ab19-f30952b00db4 | docs | python | python_datamodel | 39 | 92 | * the class body is executed;
* the class object is created.
#### 3.3.3.2. Resolving MRO entries[¶](#resolving-mro-entries "Permalink to this headline")
If a base that appears in class definition is not an instance of [`type`](../library/functions.html#type "type"),
then an `__mro_entries__` method is searched on it.... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
aa030d42-265c-55c5-a8b2-6d2f72b3cfb8 | docs | python | python_datamodel | 39 | 93 | * if an explicit metaclass is given and it is *not* an instance of
[`type()`](../library/functions.html#type "type"), then it is used directly as the metaclass;
* if an instance of [`type()`](../library/functions.html#type "type") is given as the explicit metaclass, or
bases are defined, then the most derived metac... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
febe7402-75ff-5f46-8574-390a336571e8 | docs | python | python_datamodel | 39 | 94 | as `namespace = metaclass.__prepare__(name, bases, **kwds)` (where the
additional keyword arguments, if any, come from the class definition). The
`__prepare__` method should be implemented as a
[`classmethod`](../library/functions.html#classmethod "classmethod"). The
namespace returned by `__prepare__` is passed in to ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
589e4d42-00ba-5e9f-afd7-6db5d792b660 | docs | python | python_datamodel | 39 | 95 | any methods) to reference names from the current and outer scopes when the
class definition occurs inside a function.
However, even when the class definition occurs inside the function, methods
defined inside the class still cannot see names defined at the class scope.
Class variables must be accessed through the firs... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e2f8dde6-2d35-580e-8701-16b73b371cd4 | docs | python | python_datamodel | 39 | 96 | created by the compiler if any methods in a class body refer to either
`__class__` or `super`. This allows the zero argument form of
[`super()`](../library/functions.html#super "super") to correctly identify the class being defined based on
lexical scoping, while the class or instance that was used to make the
current ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
3934aad9-6db5-5ce9-8093-1d405162b8d5 | docs | python | python_datamodel | 39 | 97 | * first, `type.__new__` collects all of the descriptors in the class
namespace that define a [`__set_name__()`](#object.__set_name__ "object.__set_name__") method;
* second, all of these `__set_name__` methods are called with the class
being defined and the assigned name of that particular descriptor;
* finally, th... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
db2c0ab0-101e-5992-8658-8d7d05230a31 | docs | python | python_datamodel | 39 | 98 | See also
[**PEP 3135**](https://www.python.org/dev/peps/pep-3135) - New super
: Describes the implicit `__class__` closure reference
#### 3.3.3.7. Uses for metaclasses[¶](#uses-for-metaclasses "Permalink to this headline")
The potential uses for metaclasses are boundless. Some ideas that have been
explored include... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
dedf0cee-4a7d-59bd-9197-18995c9322bf | docs | python | python_datamodel | 39 | 99 | order to allow the addition of Abstract Base Classes (ABCs) as “virtual base
classes” to any class or type (including built-in types), including other
ABCs.
`class.``__instancecheck__`(*self*, *instance*)[¶](#class.__instancecheck__ "Permalink to this definition")
: Return true if *instance* should be considered a (... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
cec7c2bb-5c8b-5866-b1e9-869ff90e1c3d | docs | python | python_datamodel | 39 | 100 | [**PEP 3119**](https://www.python.org/dev/peps/pep-3119) - Introducing Abstract Base Classes
: Includes the specification for customizing [`isinstance()`](../library/functions.html#isinstance "isinstance") and
[`issubclass()`](../library/functions.html#issubclass "issubclass") behavior through [`__instancecheck__... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
f63fdc37-e3c1-50a6-bac8-84444e77080a | docs | python | python_datamodel | 39 | 101 | For example, the annotation `list[int]` might be used to signify a
[`list`](../library/stdtypes.html#list "list") in which all the elements are of type [`int`](../library/functions.html#int "int").
See also
[**PEP 484**](https://www.python.org/dev/peps/pep-0484) - Type Hints
: Introducing Python’s framework for typ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
20cb727a-13dc-5df4-8c0d-9bf5697dffbe | docs | python | python_datamodel | 39 | 102 | *classmethod* `object.``__class_getitem__`(*cls*, *key*)[¶](#object.__class_getitem__ "Permalink to this definition")
: Return an object representing the specialization of a generic class
by type arguments found in *key*.
When defined on a class, `__class_getitem__()` is automatically a class
method. As ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
ff3f491c-cb70-580d-823c-497005af57c1 | docs | python | python_datamodel | 39 | 103 | library class that already implements [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__"), or
inherit from [`typing.Generic`](../library/typing.html#typing.Generic "typing.Generic"), which has its own implementation of
`__class_getitem__()`.
Custom implementations of [`__class_getitem__()`](#... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
3baa1b35-abc6-5d8d-bffc-9a1f72ac6faa | docs | python | python_datamodel | 39 | 104 | the class method [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__") may be called instead.
`__class_getitem__()` should return a [GenericAlias](../library/stdtypes.html#types-genericalias)
object if it is properly defined.
Presented with the [expression](../glossary.html#term-expression) `ob... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
92872460-21ca-5f67-86de-65fb3f0e955e | docs | python | python_datamodel | 39 | 105 | elif isclass(obj) and hasattr(obj, '__class_getitem__'):
return obj.__class_getitem__(x)
# Else, raise an exception
else:
raise TypeError(
f"'{class_of_obj.__name__}' object is not subscriptable"
)
```
In Python, all classes are themselves instances of other classes. Th... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
ecca909a-fb1a-55d6-b9ad-f56a5c4d6cd3 | docs | python | python_datamodel | 39 | 106 | >>> # "list[int]" calls "list.__class_getitem__(int)"
>>> list[int]
list[int]
>>> # list.__class_getitem__ returns a GenericAlias object:
>>> type(list[int])
<class 'types.GenericAlias'>
```
However, if a class has a custom metaclass that defines
[`__getitem__()`](#object.__getitem__ "object.__getitem__"), subscribing... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
2b0d5cee-7f8b-5f75-a242-85b304ade6f3 | docs | python | python_datamodel | 39 | 107 | [**PEP 560**](https://www.python.org/dev/peps/pep-0560) - Core Support for typing module and generic types
: Introducing [`__class_getitem__()`](#object.__class_getitem__ "object.__class_getitem__"), and outlining when a
[subscription](expressions.html#subscriptions) results in `__class_getitem__()`
being cal... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
3b5258cb-af96-523b-90fe-719b11068fea | docs | python | python_datamodel | 39 | 108 | usually are [sequences](../glossary.html#term-sequence) (such as [`lists`](../library/stdtypes.html#list "list") or
[`tuples`](../library/stdtypes.html#tuple "tuple")) or [mappings](../glossary.html#term-mapping) (like
[`dictionaries`](../library/stdtypes.html#dict "dict")),
but can represent other containers as well. ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
6e6986f7-abdb-5c7e-8494-401fe03951e6 | docs | python | python_datamodel | 39 | 109 | objects. The [`collections.abc`](../library/collections.abc.html#module-collections.abc "collections.abc: Abstract base classes for containers") module provides a
[`MutableMapping`](../library/collections.abc.html#collections.abc.MutableMapping "collections.abc.MutableMapping")
[abstract base class](../glossary.html#te... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
dff0c592-5960-5852-8660-d3e10f6d9610 | docs | python | python_datamodel | 39 | 110 | [`__add__()`](#object.__add__ "object.__add__"), [`__radd__()`](#object.__radd__ "object.__radd__"), [`__iadd__()`](#object.__iadd__ "object.__iadd__"),
[`__mul__()`](#object.__mul__ "object.__mul__"), [`__rmul__()`](#object.__rmul__ "object.__rmul__") and [`__imul__()`](#object.__imul__ "object.__imul__")
described be... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9beec8d6-697e-50af-9ff3-dd3546683b2f | docs | python | python_datamodel | 39 | 111 | `object.``__len__`(*self*)[¶](#object.__len__ "Permalink to this definition")
: Called to implement the built-in function [`len()`](../library/functions.html#len "len"). Should return the length
of the object, an integer `>=` 0. Also, an object that doesn’t define a
[`__bool__()`](#object.__bool__ "object.__b... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
fbc5eec7-ff0d-5192-9a1f-529f31989923 | docs | python | python_datamodel | 39 | 112 | `object.``__length_hint__`(*self*)[¶](#object.__length_hint__ "Permalink to this definition")
: Called to implement [`operator.length_hint()`](../library/operator.html#operator.length_hint "operator.length_hint"). Should return an estimated
length for the object (which may be greater or less than the actual lengt... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e70c2abc-a390-5192-bc30-4e882005a33f | docs | python | python_datamodel | 39 | 113 | : Called to implement evaluation of `self[key]`. For [sequence](../glossary.html#term-sequence) types,
the accepted keys should be integers and slice objects. Note that the
special interpretation of negative indexes (if the class wishes to emulate a
[sequence](../glossary.html#term-sequence) type) is up t... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0d2295d4-fb36-5957-ae65-9be5ce8a4942 | docs | python | python_datamodel | 39 | 114 | [`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_39.md |
2ef1c715-ba27-5f8e-80bd-60a14ce6a30f | docs | python | python_datamodel | 39 | 115 | for improper *key* values as for the [`__getitem__()`](#object.__getitem__ "object.__getitem__") method.
`object.``__delitem__`(*self*, *key*)[¶](#object.__delitem__ "Permalink to this definition")
: Called to implement deletion of `self[key]`. Same note as for
[`__getitem__()`](#object.__getitem__ "object._... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
6fdf3c5b-6024-5e5c-9b27-9682cb29e133 | docs | python | python_datamodel | 39 | 116 | : This method is called when an 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.
Iterator objects also need to implement this method; they are requi... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
77f48a53-9f70-5b3e-9994-4fc6ef05d5c2 | docs | python | python_datamodel | 39 | 117 | built-in will fall back to using the sequence protocol ([`__len__()`](#object.__len__ "object.__len__") and
[`__getitem__()`](#object.__getitem__ "object.__getitem__")). Objects that support the sequence protocol should
only provide [`__reversed__()`](#object.__reversed__ "object.__reversed__") if they can ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
2ea2c8df-54e6-587b-aec2-c892354e369a | docs | python | python_datamodel | 39 | 118 | is in *self*, false otherwise. For mapping objects, this should consider the
keys of the mapping rather than the values or the key-item pairs.
For objects that don’t define [`__contains__()`](#object.__contains__ "object.__contains__"), the membership test first
tries iteration via [`__iter__()`](#obje... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e3ab6672-37ce-5153-9a04-1f4e7c2d8e79 | docs | python | python_datamodel | 39 | 119 | `object.``__sub__`(*self*, *other*)[¶](#object.__sub__ "Permalink to this definition")
`object.``__mul__`(*self*, *other*)[¶](#object.__mul__ "Permalink to this definition")
`object.``__matmul__`(*self*, *other*)[¶](#object.__matmul__ "Permalink to this definition")
`object.``__truediv__`(*self*, *other*)[¶](#object... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
02f78a72-5b8d-5937-a660-042c6f4cef61 | docs | python | python_datamodel | 39 | 120 | `object.``__xor__`(*self*, *other*)[¶](#object.__xor__ "Permalink to this definition")
`object.``__or__`(*self*, *other*)[¶](#object.__or__ "Permalink to this definition")
: These methods are called to implement the binary arithmetic operations
(`+`, `-`, `*`, `@`, `/`, `//`, `%`, [`divmod()`](../library/functio... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
d33982f0-e36a-5314-9e96-906dfc790b12 | docs | python | python_datamodel | 39 | 121 | an optional third argument if the ternary version of the built-in [`pow()`](../library/functions.html#pow "pow")
function is to be supported.
If one of those methods does not support the operation with the supplied
arguments, it should return `NotImplemented`.
`object.``__radd__`(*self*, *other*)[¶](#... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
130462fd-e581-5644-9868-9d42a9d7865b | docs | python | python_datamodel | 39 | 122 | `object.``__rdivmod__`(*self*, *other*)[¶](#object.__rdivmod__ "Permalink to this definition")
`object.``__rpow__`(*self*, *other*[, *modulo*])[¶](#object.__rpow__ "Permalink to this definition")
`object.``__rlshift__`(*self*, *other*)[¶](#object.__rlshift__ "Permalink to this definition")
`object.``__rrshift__`(*se... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
bac411d4-d121-59c5-8fc5-707c30e2dfa6 | docs | python | python_datamodel | 39 | 123 | not support the corresponding operation [3](#id10) and the operands are of different
types. [4](#id11) For instance, to evaluate the expression `x - y`, where *y* is
an instance of a class that has an [`__rsub__()`](#object.__rsub__ "object.__rsub__") method, `y.__rsub__(x)`
is called if `x.__sub__(y)` ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
dfb8470b-f38f-5bec-b74a-b0dd956b03a3 | docs | python | python_datamodel | 39 | 124 | `object.``__isub__`(*self*, *other*)[¶](#object.__isub__ "Permalink to this definition")
`object.``__imul__`(*self*, *other*)[¶](#object.__imul__ "Permalink to this definition")
`object.``__imatmul__`(*self*, *other*)[¶](#object.__imatmul__ "Permalink to this definition")
`object.``__itruediv__`(*self*, *other*)[¶](... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
7bee8e46-5254-5b60-80b3-092ca93d5a32 | docs | python | python_datamodel | 39 | 125 | `object.``__ixor__`(*self*, *other*)[¶](#object.__ixor__ "Permalink to this definition")
`object.``__ior__`(*self*, *other*)[¶](#object.__ior__ "Permalink to this definition")
: These methods are called to implement the augmented arithmetic assignments
(`+=`, `-=`, `*=`, `@=`, `/=`, `//=`, `%=`, `**=`, `<<=`,
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
d9daea09-66df-5e97-8846-967268511e24 | docs | python | python_datamodel | 39 | 126 | [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.
Note
Due to a bug in the dispatching mechanism for `**=`, a class that
defines [`__ipow__()`](#object.... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
e49f38fb-d069-5af7-b980-bbc655e72902 | docs | python | python_datamodel | 39 | 127 | `object.``__int__`(*self*)[¶](#object.__int__ "Permalink to this definition")
`object.``__float__`(*self*)[¶](#object.__float__ "Permalink to this definition")
: Called to implement the built-in functions [`complex()`](../library/functions.html#complex "complex"),
[`int()`](../library/functions.html#int "int") a... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
70c683cb-f853-5842-bd96-a36fb3b99a85 | docs | python | python_datamodel | 39 | 128 | If [`__int__()`](#object.__int__ "object.__int__"), [`__float__()`](#object.__float__ "object.__float__") and [`__complex__()`](#object.__complex__ "object.__complex__") are not
defined then corresponding built-in functions [`int()`](../library/functions.html#int "int"), [`float()`](../library/functions.html#fl... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0f9d987f-aa53-5960-bfd9-3925bfdc684a | docs | python | python_datamodel | 39 | 129 | functions [`trunc()`](../library/math.html#math.trunc "math.trunc"), [`floor()`](../library/math.html#math.floor "math.floor") and [`ceil()`](../library/math.html#math.ceil "math.ceil").
Unless *ndigits* is passed to `__round__()` all these methods should
return the value of the object truncated to an [`Int... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
9f61f5fb-a159-5139-99d1-82aa9907fad6 | docs | python | python_datamodel | 39 | 130 | 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_39.md |
2fe282ce-696e-5268-abb6-f83fb9012a10 | docs | python | python_datamodel | 39 | 131 | : 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 will be [`None`](../library/constants.html#None "None").
If an exception is supplied, and the method wishes to... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
1989f29e-cce5-55be-b1c7-ba78eecf2a90 | docs | python | python_datamodel | 39 | 132 | ### 3.3.10. Special method lookup[¶](#special-method-lookup "Permalink to this headline")
For custom classes, implicit invocations of special methods are only guaranteed
to work correctly if defined on an object’s type, not in the object’s instance
dictionary. That behaviour is the reason why the following code raises... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
60bc64ae-1fea-5102-9a13-ab4f48825858 | docs | python | python_datamodel | 39 | 133 | Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__hash__' of 'int' object needs an argument
```
Incorrectly attempting to invoke an unbound method of a class in this way is
sometimes referred to as ‘metaclass confusion’, and is avoided by bypassing
the instance when look... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
0fa717ba-2a9a-59e0-85d3-f48b5cb3505a | docs | python | python_datamodel | 39 | 134 | ... print("Class getattribute invoked")
... return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__() # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c) # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c) ... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
b106dbe1-a5cb-5df6-b174-c2896ae16cc9 | docs | python | python_datamodel | 39 | 135 | [Coroutine objects](../glossary.html#term-coroutine) returned from [`async def`](compound_stmts.html#async-def) functions
are awaitable.
Note
The [generator iterator](../glossary.html#term-generator-iterator) objects returned from generators
decorated with [`types.coroutine()`](../library/types.html#types.coroutine "... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
39633656-0dd5-58f6-9a35-4fa303fcd397 | docs | python | python_datamodel | 39 | 136 | [**PEP 492**](https://www.python.org/dev/peps/pep-0492) for additional information about awaitable objects.
### 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 exec... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
c64f8f8e-48da-5be6-b4c8-ebc82f5d6d5d | docs | python | python_datamodel | 39 | 137 | those of generators (see [Generator-iterator methods](expressions.html#generator-methods)). However, unlike
generators, coroutines do not directly support iteration.
Changed in version 3.5.2: It is a [`RuntimeError`](../library/exceptions.html#RuntimeError "RuntimeError") to await on a coroutine more than once.
`coro... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
20248b56-394f-5a40-a893-863c59940dc1 | docs | python | python_datamodel | 39 | 138 | iterating over the [`__await__()`](#object.__await__ "object.__await__") return value, described above.
`coroutine.``throw`(*value*)[¶](#coroutine.throw "Permalink to this definition")
`coroutine.``throw`(*type*[, *value*[, *traceback*]])
: Raises the specified exception in the coroutine. This method delegates
... | large_dataset/raw/docs/python/datamodel/datamodel_39.md |
a45ed1b2-0204-539b-846b-ccd6c2f365d6 | docs | python | python_datamodel | 39 | 139 | : Causes the coroutine to clean itself up and exit. If the coroutine
is suspended, this method first delegates to the [`close()`](expressions.html#generator.close "generator.close")
method of the iterator that caused the coroutine to suspend, if it
has such a method. Then it raises [`GeneratorExit`](../li... | 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.