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 |
|---|---|---|---|---|---|---|---|
668e9a27-b246-5597-b7df-1c2960107a32 | docs | python | python_datamodel | 315 | 161 | [`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_315.md |
9c53900e-0124-57ff-bd25-c0a66913b330 | docs | python | python_datamodel | 315 | 162 | 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_315.md |
c5978339-701b-5322-bc9b-1bcc9b7a7440 | docs | python | python_datamodel | 315 | 163 | 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")).
Changed in version 3.14: [`int()`](../library/functi... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
a39d00ad-871e-5849-850d-e5624a391de7 | docs | python | python_datamodel | 315 | 164 | Typical uses of context managers include saving and restoring various kinds of
global state, locking and unlocking resources, closing opened files, etc.
For more information on context managers, see [Context Manager Types](../library/stdtypes.html#typecontextmanager).
The [`object`](../library/functions.html#object "o... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
687d7ad2-f396-5992-8b72-5576de8bbe1f | docs | python | python_datamodel | 315 | 165 | without an exception, all three arguments will be [`None`](../library/constants.html#None "None").
If an exception is supplied, and the method wishes to suppress the exception
(i.e., prevent it from being propagated), it should return a true value.
Otherwise, the exception will be processed normally up... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
1f3fff1f-b6d6-537a-b798-09010de84fed | docs | python | python_datamodel | 315 | 166 | allowed by default, i.e. `case MyClass(x, y)` is typically invalid without special
support in `MyClass`. To be able to use that kind of pattern, the class needs to
define a *\_\_match\_args\_\_* attribute.
object.\_\_match\_args\_\_[¶](#object.__match_args__ "Link to this definition")
: This class variable can be as... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
c6c9dc23-67c0-5169-8775-e9b0cf63dd01 | docs | python | python_datamodel | 315 | 167 | a [`TypeError`](../library/exceptions.html#TypeError "TypeError").
Added in version 3.10.
See also
[**PEP 634**](https://peps.python.org/pep-0634/) - Structural Pattern Matching
: The specification for the Python `match` statement.
### 3.3.11. Emulating buffer types[¶](#emulating-buffer-types "Link to this headin... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
2a7392d4-ec46-5abf-a6c3-9105576b0f29 | docs | python | python_datamodel | 315 | 168 | [`memoryview`](../library/stdtypes.html#memoryview "memoryview") constructor). The *flags* argument is an integer
representing the kind of buffer requested, affecting for example whether
the returned buffer is read-only or writable. [`inspect.BufferFlags`](../library/inspect.html#inspect.BufferFlags "inspec... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
7f02e158-1ebe-593f-9423-6fc11cdd12c8 | docs | python | python_datamodel | 315 | 169 | to implement this method.
Added in version 3.12.
See also
[**PEP 688**](https://peps.python.org/pep-0688/) - Making the buffer protocol accessible in Python
: Introduces the Python `__buffer__` and `__release_buffer__` methods.
[`collections.abc.Buffer`](../library/collections.abc.html#collections.abc.Buffer ... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
31717ed6-81f1-55ac-8684-ebdae66bdfed | docs | python | python_datamodel | 315 | 170 | attribute is set to a dictionary mapping from variable names to annotations.
Changed in version 3.14: Annotations are now lazily evaluated.
object.\_\_annotate\_\_(*format*)[¶](#object.__annotate__ "Link to this definition")
: An [annotate function](../glossary.html#term-annotate-function).
Returns a ne... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
d7fbeacc-bec4-50fd-85a9-720829f00a95 | docs | python | python_datamodel | 315 | 171 | [`VALUE`](../library/annotationlib.html#annotationlib.Format.VALUE "annotationlib.Format.VALUE") format; they must not raise
[`NotImplementedError()`](../library/exceptions.html#NotImplementedError "NotImplementedError") when called with this format.
When called with [`VALUE`](../library/annotationlib.html... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
00c9987d-7e37-5d71-aac4-8e0ee6bac285 | docs | python | python_datamodel | 315 | 172 | ### 3.3.13. Special method lookup[¶](#special-method-lookup "Link to this heading")
For custom classes, implicit invocations of special methods are only guaranteed
to work correctly if defined on an object’s type, not in the object’s instance
dictionary. That behaviour is the reason why the following code raises an
ex... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
732ad2db-de4c-533a-b8bb-c637648c8b21 | docs | python | python_datamodel | 315 | 173 | 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_315.md |
c1336915-cd56-5f63-8618-48d3cc40160d | docs | python | python_datamodel | 315 | 174 | ... 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_315.md |
e4804fb5-4510-5fa8-976f-1e477699ec64 | docs | python | python_datamodel | 315 | 175 | [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_315.md |
5ac65cb0-c5d5-5316-bf94-594d540c652d | docs | python | python_datamodel | 315 | 176 | The language doesn’t place any restriction on the type or value of the
objects yielded by the iterator returned by `__await__`, as this is
specific to the implementation of the asynchronous execution framework
(e.g. [`asyncio`](../library/asyncio.html#module-asyncio "asyncio: Asynchronous I/O.")) that w... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
a1a305fb-cc37-5fc1-89c9-434daf86b667 | docs | python | python_datamodel | 315 | 177 | returns, the iterator raises [`StopIteration`](../library/exceptions.html#StopIteration "StopIteration"), and the exception’s
[`value`](../library/exceptions.html#StopIteration.value "StopIteration.value") attribute holds the return value. If the
coroutine raises an exception, it is propagated by the iterator. Coroutin... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
06c248e4-cf64-51ab-9173-79957f32db16 | docs | python | python_datamodel | 315 | 178 | this is equivalent to advancing the iterator returned by
[`__await__()`](#object.__await__ "object.__await__"). If *value* is not `None`, this method delegates
to the [`send()`](expressions.html#generator.send "generator.send") method of the iterator that caused
the coroutine to suspend. The result (ret... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
4707c415-968e-5109-906e-2e2488f861a7 | docs | python | python_datamodel | 315 | 179 | (return value, [`StopIteration`](../library/exceptions.html#StopIteration "StopIteration"), or other exception) is the same as
when iterating over the [`__await__()`](#object.__await__ "object.__await__") return value, described
above. If the exception is not caught in the coroutine, it propagates
back ... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
25d65295-d17c-569b-ab21-8607f0f439e4 | docs | python | python_datamodel | 315 | 180 | Finally, the coroutine is marked as having finished executing, even if
it was never started.
Coroutine objects are automatically closed using the above process when
they are about to be destroyed.
### 3.4.3. Asynchronous Iterators[¶](#asynchronous-iterators "Link to this heading")
An *asynchronous it... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
7450b020-df3f-585c-8a41-2390b9efe12c | docs | python | python_datamodel | 315 | 181 | An example of an asynchronous iterable object:
```None
class Reader:
async def readline(self):
...
def __aiter__(self):
return self
async def __anext__(self):
val = await self.readline()
if val == b'':
raise StopAsyncIteration
return val
```
Added in v... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
566acebf-9074-51f5-ad7e-aff024ceb7ef | docs | python | python_datamodel | 315 | 182 | suspend execution in its `__aenter__` and `__aexit__` methods.
Asynchronous context managers can be used in an [`async with`](compound_stmts.html#async-with) statement.
The [`object`](../library/functions.html#object "object") class itself does not provide these methods.
object.\_\_aenter\_\_(*self*)[¶](#object.__ae... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
9b598761-f3d6-5110-a783-1dfeca57bc18 | docs | python | python_datamodel | 315 | 183 | Added in version 3.5.
Footnotes
[[1](#id1)]
It *is* possible in some cases to change an object’s type, under certain
controlled conditions. It generally isn’t a good idea though, since it can
lead to some very strange behaviour if it is handled incorrectly.
[[2](#id12)]
The [`__hash__()`](#object.__hash__ "object... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
1f4c2e8b-810b-569b-9e2c-5f514e8e27bc | docs | python | python_datamodel | 315 | 184 | the method returns [`NotImplemented`](../library/constants.html#NotImplemented "NotImplemented"). Do not set the method to
`None` if you want to force fallback to the right operand’s reflected
method—that will instead have the opposite effect of explicitly
*blocking* such fallback.
[[4](#id17)]
For operands of the s... | large_dataset/raw/docs/python/datamodel/datamodel_315.md |
23106752-1d50-55b8-b6f8-41dc3be6aa83 | docs | python | python_datamodel | 38 | 0 | # 3.Data model¶
Source: https://docs.python.org/3.8/reference/datamodel.html
Version: 3.8
## 3.1. Objects, values and types[¶](#objects-values-and-types "Permalink to this headline")
*Objects* are Python’s abstraction for data. All data in a Python program
is represented by objects or by relations between objects. (... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a5833787-e91b-51f8-8c30-cf11e41d5ae1 | docs | python | python_datamodel | 38 | 1 | it have a length?”) and also defines the possible values for objects of that
type. The [`type()`](../library/functions.html#type "type") function returns an object’s type (which is an object
itself). Like its identity, an object’s *type* is also unchangeable.
[1](#id8)
The *value* of some objects can change. Objects w... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8913661a-3e6c-597a-8a81-14ab56ca42ad | docs | python | python_datamodel | 38 | 2 | Objects are never explicitly destroyed; however, when they become unreachable
they may be garbage-collected. An implementation is allowed to postpone garbage
collection or omit it altogether — it is a matter of implementation quality
how garbage collection is implemented, as long as no objects are collected that
are st... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e795f30c-b4f9-563b-a07b-497ec0d57f54 | docs | python | python_datamodel | 38 | 3 | Note that the use of the implementation’s tracing or debugging facilities may
keep objects alive that would normally be collectable. Also note that catching
an exception with a ‘[`try`](compound_stmts.html#try)…[`except`](compound_stmts.html#except)’ statement may keep
objects alive.
Some objects contain references to... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
b5fdc3c0-aa27-5a0a-8300-c40c01440375 | docs | python | python_datamodel | 38 | 4 | Examples of containers are tuples, lists and dictionaries. The references are
part of a container’s value. In most cases, when we talk about the value of a
container, we imply the values, not the identities of the contained objects;
however, when we talk about the mutability of a container, only the identities
of the i... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
f64141b6-92d9-57ae-b6b6-664136ca71d9 | docs | python | python_datamodel | 38 | 5 | []`, `c` and `d` are guaranteed to refer to two different, unique, newly
created empty lists. (Note that `c = d = []` assigns the same object to both
`c` and `d`.)
## 3.2. The standard type hierarchy[¶](#the-standard-type-hierarchy "Permalink to this headline")
Below is a list of the types that are built into Python.... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
ceaad585-3ec2-5b6f-aef3-a177c97ba4e6 | docs | python | python_datamodel | 38 | 6 | object is accessed through the built-in name `None`. It is used to signify the
absence of a value in many situations, e.g., it is returned from functions that
don’t explicitly return anything. Its truth value is false.
NotImplemented
: This type has a single value. There is a single object with this valu... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a8c0462c-167e-5f24-bfc5-1c6c976c38a1 | docs | python | python_datamodel | 38 | 7 | [`numbers.Number`](../library/numbers.html#numbers.Number "numbers.Number")
: These are created by numeric literals and returned as results by arithmetic
operators and arithmetic built-in functions. Numeric objects are immutable;
once created their value never changes. Python numbers are of course strongly
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8fe0d84f-8525-5c86-97ca-fd70021ca91a | docs | python | python_datamodel | 38 | 8 | decimal point, are not shown.
* A sign is shown only when the number is negative.
Python distinguishes between integers, floating point numbers, and complex
numbers:
[`numbers.Integral`](../library/numbers.html#numbers.Integral "numbers.Integral")
: These represent elements from the mathem... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
472ca5ea-a5f7-5d27-912b-cce65b8951dc | docs | python | python_datamodel | 38 | 9 | 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... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6f164987-d4da-5111-8a6f-5aa92c707146 | docs | python | python_datamodel | 38 | 10 | memory usage that are usually the reason for using these are dwarfed by the
overhead of using objects in Python, so there is no reason to complicate the
language with two kinds of floating point numbers.
[`numbers.Complex`](../library/numbers.html#numbers.Complex "numbers.Complex") ([`compl... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
69c5f035-0a8d-53de-a149-3ae562f3204e | docs | python | python_datamodel | 38 | 11 | …, *n*-1. Item *i* of sequence *a* is selected by `a[i]`.
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. This implies that the index set is renumbered so
that it starts at ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
47441d20-85c7-5708-b9e6-c766ed23fdb1 | docs | python | python_datamodel | 38 | 12 | : 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
obje... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
7703ddfe-4b96-5910-a942-ac4821ff8b71 | docs | python | python_datamodel | 38 | 13 | [`bytes.decode()`](../library/stdtypes.html#bytes.decode "bytes.decode") can be used to achieve the opposite.
Tuples
: 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 ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
c3186f5e-a419-53a7-b88a-d8ed388087d6 | docs | python | python_datamodel | 38 | 14 | decoded to strings via the [`decode()`](../library/stdtypes.html#bytes.decode "bytes.decode") method.
Mutable sequences
: 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#d... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8afc0db3-c0c4-57f0-a170-aff0c8d28d87 | docs | python | python_datamodel | 38 | 15 | and functionality as immutable [`bytes`](../library/stdtypes.html#bytes "bytes") objects.
The extension module [`array`](../library/array.html#module-array "array: Space efficient arrays of uniformly typed numeric values.") provides an additional example of a
mutable sequence type, as does ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
1b20dc4c-5e0b-55a2-af0b-d5995c05edf6 | docs | python | python_datamodel | 38 | 16 | that numeric types obey the normal rules for numeric comparison: if two numbers
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()`](../li... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
c028b1aa-e45f-5e1a-a308-fdc639f2a982 | docs | python | python_datamodel | 38 | 17 | `a`; this can be used in expressions and as the target of assignments or
[`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:
Dictionaries
: ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
d6a2f65f-511c-5c34-a0b6-746b4ad2feed | docs | python | python_datamodel | 38 | 18 | in the same order they were added sequentially over the dictionary.
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 `{...}` notat... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
390cecfc-60b1-5a22-b619-23def9fa7020 | docs | python | python_datamodel | 38 | 19 | an implementation detail at that time rather than a language guarantee.
Callable types
: These are the types to which the function call operation (see section
[Calls](expressions.html#calls)) can be applied:
User-defined functions
: A user-defined function object is created by a function defin... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
13abd772-e4e1-5801-8a32-b44121bc8eb1 | docs | python | python_datamodel | 38 | 20 | | [`__qualname__`](../library/stdtypes.html#definition.__qualname__ "definition.__qualname__") | The function’s [qualified name](../glossary.html#term-qualified-name). New in version 3.3. | Writable |
| `__module__` | The name of the module the function was defined in, or `None` if unavailable. | Writa... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6e7da261-5f2b-550c-b4ad-d2894f591a6b | docs | python | python_datamodel | 38 | 21 | | `__closure__` | `None` or a tuple of cells that contain bindings for the function’s free variables. See below for information on the `cell_contents` attribute. | Read-only |
| `__annotations__` | A dict containing annotations of parameters. The keys of the dict are the parameter names, and `'return'` ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
dbea5dc8-6a07-58de-8749-c1209fe1b3c8 | docs | python | python_datamodel | 38 | 22 | A cell object has the attribute `cell_contents`. This can be used to get
the value of the cell, as well as set the value.
Additional information about a function’s definition can be retrieved from its
code object; see the description of internal types below. The
[`cell`](../libr... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e0cad443-ba28-5e25-aa0c-c653ed339fca | docs | python | python_datamodel | 38 | 23 | name of the module the method was defined in, or `None` if unavailable.
Methods also support accessing (but not setting) the arbitrary function
attributes on the underlying function object.
User-defined method objects may be created when getting an attribute of a
class (perhaps... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
f9b4d9b1-2a43-58dd-9177-3dc7ac9a88a7 | docs | python | python_datamodel | 38 | 24 | underlying the class method.
When an instance method object is called, the underlying function
(`__func__`) is called, inserting the class instance
(`__self__`) in front of the argument list. For instance, when
`C` is a class which contains a definition for a function
`f... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
aa32e052-9de4-55c3-8aca-08adfb8af2a7 | docs | python | python_datamodel | 38 | 25 | variable and call that local variable. Also notice that this
transformation only happens for user-defined functions; other callable
objects (and all non-callable objects) are retrieved without
transformation. It is also important to note that user-defined functions
which are attr... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6cc3d871-bb0d-52dd-8923-6ee45e201f85 | docs | python | python_datamodel | 38 | 26 | using the `yield` statement. When the function executes a
[`return`](simple_stmts.html#return) statement or falls off the end, a [`StopIteration`](../library/exceptions.html#StopIteration "StopIteration")
exception is raised and the iterator will have reached the end of the set of
values... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
828d2587-b167-5555-a5d1-4afc2467a8c3 | docs | python | python_datamodel | 38 | 27 | which uses the [`yield`](simple_stmts.html#yield) statement is called a
*asynchronous generator function*. Such a function, when called,
returns an asynchronous iterator object which can be used in an
[`async for`](compound_stmts.html#async-for) statement to execute the body of the funct... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
2bcc3cc8-7494-58ca-aa52-e2f77aebfa30 | docs | python | python_datamodel | 38 | 28 | : A built-in function object is a wrapper around a C function. Examples of
built-in functions are [`len()`](../library/functions.html#len "len") and [`math.sin()`](../library/math.html#math.sin "math.sin") ([`math`](../library/math.html#module-math "math: Mathematical functions (sin() etc.).") is a
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a8edd1e8-6d8c-545a-8538-3476ff18af4d | docs | python | python_datamodel | 38 | 29 | a built-in method is `alist.append()`, assuming *alist* is a list object. In
this case, the special read-only attribute `__self__` is set to the object
denoted by *alist*.
Classes
: Classes are callable. These objects normally act as factories for new
instances of themselves, ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
3ab2da1a-d30c-5831-9b05-77a4be51e922 | docs | python | python_datamodel | 38 | 30 | [`import`](simple_stmts.html#import) statement, or by calling
functions such as [`importlib.import_module()`](../library/importlib.html#importlib.import_module "importlib.import_module") and built-in
[`__import__()`](../library/functions.html#__import__ "__import__"). A module object has a namespace impleme... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
3a583901-eacd-5287-9e75-d9299dc83542 | docs | python | python_datamodel | 38 | 31 | `__doc__` is the module’s documentation string, or `None` if
unavailable; `__annotations__` (optional) is a dictionary containing
[variable annotations](../glossary.html#term-variable-annotation) collected during module
body execution; [`__file__`](import.html#__file__ "__file__") is the pathname of the... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
59548e28-8d1a-5944-8774-181b8ecadaff | docs | python | python_datamodel | 38 | 32 | dictionary will be cleared when the module falls out of scope even if the
dictionary still has live references. To avoid this, copy the dictionary
or keep the module around while using its dictionary directly.
Custom classes
: Custom class types are typically created by class definitions (see section
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
b7709f23-d8a9-5fa8-9eb6-b06c96ec254f | docs | python | python_datamodel | 38 | 33 | Additional details on the C3 MRO used by Python can be found in the
documentation accompanying the 2.3 release at
<https://www.python.org/download/releases/2.3/mro/>.
When a class attribute reference (for class `C`, say) would yield a
class method object, it is transformed into an instance method o... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
3f92e31f-2e05-587e-9284-e8409d0a5f0b | docs | python | python_datamodel | 38 | 34 | Special attributes: [`__name__`](../library/stdtypes.html#definition.__name__ "definition.__name__") is the class name; `__module__` is
the module name in which the class was defined; [`__dict__`](../library/stdtypes.html#object.__dict__ "object.__dict__") is the
dictionary containing the class’s namespace;... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
b0d1cecb-c8c3-5b16-ad81-55e314f117c9 | docs | python | python_datamodel | 38 | 35 | there, and the instance’s class has an attribute by that name, the search
continues with the class attributes. If a class attribute is found that is a
user-defined function object, it is transformed into an instance method
object whose `__self__` attribute is the instance. Static method and
class me... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
eb950323-d1e3-5ea5-b292-6d23c47c3b73 | docs | python | python_datamodel | 38 | 36 | [`__delattr__()`](#object.__delattr__ "object.__delattr__") method, this is called instead of updating the instance
dictionary directly.
Class instances can pretend to be numbers, sequences, or mappings if they have
methods with certain special names. See section [Special method names](#specialnames).
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a1ac3325-874e-5881-9a4b-93579f74b240 | docs | python | python_datamodel | 38 | 37 | [`makefile()`](../library/socket.html#socket.socket.makefile "socket.socket.makefile") method of socket objects (and perhaps by
other functions or methods provided by extension modules).
The objects `sys.stdin`, `sys.stdout` and `sys.stderr` are
initialized to file objects corresponding to the interpre... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a70e9cf0-fd0d-599c-a01a-0d9cf1e83daa | docs | python | python_datamodel | 38 | 38 | object contains an explicit reference to the function’s globals (the module in
which it was defined), while a code object contains no context; also the default
argument values are stored in the function object, not in the code object
(because they represent values calculated at run-time)... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
5712694e-737c-507c-8d3b-d380829a0a6b | docs | python | python_datamodel | 38 | 39 | function (including arguments); `co_varnames` is a tuple containing
the names of the local variables (starting with the argument names);
`co_cellvars` is a tuple containing the names of local variables
that are referenced by nested functions; `co_freevars` is a tuple
containing t... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
0fb8d4d0-e6e9-56de-a206-847a42560aff | docs | python | python_datamodel | 38 | 40 | The following flag bits are defined for `co_flags`: bit `0x04` is set if
the function uses the `*arguments` syntax to accept an arbitrary number of
positional arguments; bit `0x08` is set if the function uses the
`**keywords` syntax to accept arbitrary keyword arguments; bit `0x20` is se... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6cddfccc-3fef-574b-8a93-8b98b7699e41 | docs | python | python_datamodel | 38 | 41 | : Frame objects represent execution frames. They may occur in traceback objects
(see below), and are also passed to registered trace functions.
Special read-only attributes: `f_back` is to the previous stack frame
(towards the caller), or `None` if this is the bottom stack frame;
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a5610ec3-a580-5e90-ad8b-dc363163b4e3 | docs | python | python_datamodel | 38 | 42 | Normally an event is triggered for each new source line - this can be
disabled by setting `f_trace_lines` to [`False`](../library/constants.html#False "False").
Implementations *may* allow per-opcode events to be requested by setting
`f_trace_opcodes` to [`True`](../library/constants.ht... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
55ba40c1-f809-57b1-952a-20cb2b4433a7 | docs | python | python_datamodel | 38 | 43 | frame. Also, if the frame belonged to a generator, the generator
is finalized. This helps break reference cycles involving frame
objects (for example when catching an exception and storing its
traceback for later use).
[`RuntimeError`](../library/exceptions.h... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
aa76ffcd-e225-5dd1-b643-5347b08b718c | docs | python | python_datamodel | 38 | 44 | entered, the stack trace is made available to the program. (See section
[The try statement](compound_stmts.html#try).) It is accessible as the third item of the
tuple returned by `sys.exc_info()`, and as the `__traceback__` attribute
of the caught exception.
When the program con... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
cbb15d4f-8196-5fce-be25-4cd3542f03b5 | docs | python | python_datamodel | 38 | 45 | The line number and last instruction in the traceback may differ from the
line number of its frame object if the exception occurred in a
[`try`](compound_stmts.html#try) statement with no matching except clause or with a
finally clause.
Accessing `tb_frame` raises an [auditing e... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e00b65a2-0e85-55f1-956e-5d9da292fcd2 | docs | python | python_datamodel | 38 | 46 | methods. They are also created by the built-in [`slice()`](../library/functions.html#slice "slice") function.
Special read-only attributes: `start` is the lower bound;
`stop` is the upper bound; `step` is the step
value; each is `None` if omitted. These attributes can have any type.
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
5258f996-eff8-561e-b57f-a612b91ffa83 | docs | python | python_datamodel | 38 | 47 | : Static method objects provide a way of defeating the transformation of function
objects to method objects described above. A static method object is a wrapper
around any other object, usually a user-defined method object. When a static
method object is retrieved from a class or a class i... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
d2109eb8-28ab-54ee-a048-0f17f935222c | docs | python | python_datamodel | 38 | 48 | described above, under “User-defined methods”. Class method objects are created
by the built-in [`classmethod()`](../library/functions.html#classmethod "classmethod") constructor.
## 3.3. Special method names[¶](#special-method-names "Permalink to this headline")
A class can implement certain operatio... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
606da059-6923-57a6-b0ed-7645b8a62ce3 | docs | python | python_datamodel | 38 | 49 | [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError") or [`TypeError`](../library/exceptions.html#TypeError "TypeError")).
Setting a special method to `None` indicates that the corresponding
operation is not available. For example, if a class sets
[`__iter__()`](#object.__iter__ "object.__iter... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e06b2b2b-07c3-555f-add5-3b86b2a9c3c9 | docs | python | python_datamodel | 38 | 50 | ### 3.3.1. Basic customization[¶](#basic-customization "Permalink to this headline")
`object.``__new__`(*cls*[, *...*])[¶](#object.__new__ "Permalink to this definition")
: Called to create a new instance of class *cls*. [`__new__()`](#object.__new__ "object.__new__") is a static
method (special-cased so you nee... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
fb3626b4-0ab5-517f-b78a-032c4976020f | docs | python | python_datamodel | 38 | 51 | If [`__new__()`](#object.__new__ "object.__new__") is invoked during object construction and it returns an
instance of *cls*, then the new instance’s [`__init__()`](#object.__init__ "object.__init__") method
will be invoked like `__init__(self[, ...])`, where *self* is the new instance
and the remaining... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
9bd91bbb-d4ce-5f2e-bbc4-7697fb89d1f3 | docs | python | python_datamodel | 38 | 52 | : Called after the instance has been created (by [`__new__()`](#object.__new__ "object.__new__")), but before
it is returned to the caller. The arguments are those passed to the
class constructor expression. If a base class has an [`__init__()`](#object.__init__ "object.__init__")
method, the derived clas... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
ef734bc2-c663-58ee-8814-62af1799cbd8 | docs | python | python_datamodel | 38 | 53 | cause a [`TypeError`](../library/exceptions.html#TypeError "TypeError") to be raised at runtime.
`object.``__del__`(*self*)[¶](#object.__del__ "Permalink to this definition")
: Called when the instance is about to be destroyed. This is also called a
finalizer or (improperly) a destructor. If a base class has... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
2bf5ec94-07fc-5de0-9aa0-6682c9975dca | docs | python | python_datamodel | 38 | 54 | is about to be destroyed; the current [CPython](../glossary.html#term-cpython) implementation
only calls it once.
It is not guaranteed that [`__del__()`](#object.__del__ "object.__del__") methods are called for objects
that still exist when the interpreter exits.
Note
`del x` doesn’t directly... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
5e69660c-cc6e-503f-9c5f-d276339aec8e | docs | python | python_datamodel | 38 | 55 |
See also
Documentation for the [`gc`](../library/gc.html#module-gc "gc: Interface to the cycle-detecting garbage collector.") module.
Warning
Due to the precarious circumstances under which [`__del__()`](#object.__del__ "object.__del__") methods are
invoked, exceptions that occur during their ex... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
43b1caad-05e8-58cd-a3a5-829ff4333066 | docs | python | python_datamodel | 38 | 56 | modules) may already have been deleted or set to `None`. Python
guarantees that globals whose name begins with a single underscore
are deleted from their module before other globals are deleted; if
no other references to such globals exist, this may help in assuring
that imported modules a... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e4b04311-62f5-5168-b7b0-eb295e381ae3 | docs | python | python_datamodel | 38 | 57 | The return value must be a string object. If a class defines [`__repr__()`](#object.__repr__ "object.__repr__")
but not [`__str__()`](#object.__str__ "object.__str__"), then [`__repr__()`](#object.__repr__ "object.__repr__") is also used when an
“informal” string representation of instances of that class is... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a5caa71e-7e46-557a-881a-cf803d30e710 | docs | python | python_datamodel | 38 | 58 | expectation that [`__str__()`](#object.__str__ "object.__str__") return a valid Python expression: a more
convenient or concise representation can be used.
The default implementation defined by the built-in type [`object`](../library/functions.html#object "object")
calls [`object.__repr__()`](#object._... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
5f3aa1bb-ae72-5b60-a9df-c1fbe76d5384 | docs | python | python_datamodel | 38 | 59 | and by extension, evaluation of [formatted string literals](lexical_analysis.html#f-strings) and the [`str.format()`](../library/stdtypes.html#str.format "str.format") method, to produce a “formatted”
string representation of an object. The *format\_spec* argument is
a string that contains a description of ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
6dab629a-d049-52b2-96a1-528d18c89e65 | docs | python | python_datamodel | 38 | 60 | Changed in version 3.7: `object.__format__(x, '')` is now equivalent to `str(x)` rather
than `format(str(self), '')`.
`object.``__lt__`(*self*, *other*)[¶](#object.__lt__ "Permalink to this definition")
`object.``__le__`(*self*, *other*)[¶](#object.__le__ "Permalink to this definition")
`object.``__eq__`(*se... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
3461a82b-aebf-58b1-ad2e-25e78bb74359 | docs | python | python_datamodel | 38 | 61 | A rich comparison method may return the singleton `NotImplemented` if it does
not implement the operation for a given pair of arguments. By convention,
`False` and `True` are returned for a successful comparison. However, these
methods can return any value, so if the comparison operator is used in a Boo... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
de81f64e-c995-5086-9138-2e605f4fe4f7 | docs | python | python_datamodel | 38 | 62 | comparison operators or default implementations; for example, the truth of
`(x<y or x==y)` does not imply `x<=y`. To automatically generate ordering
operations from a single root operation, see [`functools.total_ordering()`](../library/functools.html#functools.total_ordering "functools.total_ordering").
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
0ab464e7-0938-5596-8ad4-469c64716e8c | docs | python | python_datamodel | 38 | 63 | [`__eq__()`](#object.__eq__ "object.__eq__") and [`__ne__()`](#object.__ne__ "object.__ne__") are their own reflection.
If the operands are of different types, and right operand’s type is
a direct or indirect subclass of the left operand’s type,
the reflected method of the right operand has priority, ot... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
9516bb2d-6efc-5291-aa6f-b57898c32ffc | docs | python | python_datamodel | 38 | 64 | advised to mix together the hash values of the components of the object that
also play a part in comparison of objects by packing them into a tuple and
hashing the tuple. Example:
```None
def __hash__(self):
return hash((self.name, self.nick, self.color))
```
Note
[`hash()`](.... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
fe3db2ea-3c15-5711-b1bb-8216ed7a0a7d | docs | python | python_datamodel | 38 | 65 | [`__hash__()`](#object.__hash__ "object.__hash__") operation either; if it defines [`__eq__()`](#object.__eq__ "object.__eq__") but not
[`__hash__()`](#object.__hash__ "object.__hash__"), its instances will not be usable as items in hashable
collections. If a class defines mutable objects and implements an
... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
8fa39a45-3521-5ff4-b94e-6eff4ace0e0a | docs | python | python_datamodel | 38 | 66 | A class that overrides [`__eq__()`](#object.__eq__ "object.__eq__") and does not define [`__hash__()`](#object.__hash__ "object.__hash__")
will have its [`__hash__()`](#object.__hash__ "object.__hash__") implicitly set to `None`. When the
[`__hash__()`](#object.__hash__ "object.__hash__") method of a class ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
bf24628c-c9fe-5e13-86a1-8aa8053cc4c7 | docs | python | python_datamodel | 38 | 67 | support, it should include `__hash__ = None` in the class definition.
A class which defines its own [`__hash__()`](#object.__hash__ "object.__hash__") that explicitly raises
a [`TypeError`](../library/exceptions.html#TypeError "TypeError") would be incorrectly identified as hashable by
an `isinstance(ob... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
62eabd04-dafd-58fc-aa7a-44c816a5c889 | docs | python | python_datamodel | 38 | 68 | Python has never made guarantees about this ordering
(and it typically varies between 32-bit and 64-bit builds).
See also [`PYTHONHASHSEED`](../using/cmdline.html#envvar-PYTHONHASHSEED).
Changed in version 3.3: Hash randomization is enabled by default.
`object.``__bool__`(*self*)[¶](#object.__bool__ ... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
66563b4b-5ff9-5070-9b23-9d98bef14448 | docs | python | python_datamodel | 38 | 69 | ### 3.3.2. Customizing attribute access[¶](#customizing-attribute-access "Permalink to this headline")
The following methods can be defined to customize the meaning of attribute
access (use of, assignment to, or deletion of `x.name`) for class instances.
`object.``__getattr__`(*self*, *name*)[¶](#object.__getattr__ "... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
c7d44828-ffbb-58b6-bd50-61ec05893b15 | docs | python | python_datamodel | 38 | 70 | attribute value or raise an [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError") exception.
Note that if the attribute is found through the normal mechanism,
[`__getattr__()`](#object.__getattr__ "object.__getattr__") is not called. (This is an intentional asymmetry between
[`... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
519b9a6a-8445-539c-b92c-d2bbf41e53a6 | docs | python | python_datamodel | 38 | 71 | `object.``__getattribute__`(*self*, *name*)[¶](#object.__getattribute__ "Permalink to this definition")
: Called unconditionally to implement attribute accesses for instances of the
class. If the class also defines [`__getattr__()`](#object.__getattr__ "object.__getattr__"), the latter will not be
called unle... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
a8abf460-ca9e-5aa4-bbde-72b9ec5b2469 | docs | python | python_datamodel | 38 | 72 | result of implicit invocation via language syntax or built-in functions.
See [Special method lookup](#special-lookup).
For certain sensitive attribute accesses, raises an
[auditing event](../library/sys.html#auditing) `object.__getattr__` with arguments
`obj` and `name`.
`object.``__setattr__`(*se... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
e3914ba4-c348-5f15-999d-c8d92e07e09c | docs | python | python_datamodel | 38 | 73 | `obj`, `name`, `value`.
`object.``__delattr__`(*self*, *name*)[¶](#object.__delattr__ "Permalink to this definition")
: Like [`__setattr__()`](#object.__setattr__ "object.__setattr__") but for attribute deletion instead of assignment. This
should only be implemented if `del obj.name` is meaningful for the ob... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
7c436802-cdb7-57f8-956d-52273762af6e | docs | python | python_datamodel | 38 | 74 | access to module attributes. The `__getattr__` function at the module level
should accept one argument which is the name of an attribute and return the
computed value or raise an [`AttributeError`](../library/exceptions.html#AttributeError "AttributeError"). If an attribute is
not found on a module object through the n... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
621f742a-f0ac-5018-85be-1fe4c7a90443 | docs | python | python_datamodel | 38 | 75 | attributes, properties, etc.), one can set the `__class__` attribute of
a module object to a subclass of [`types.ModuleType`](../library/types.html#types.ModuleType "types.ModuleType"). For example:
```None
import sys
from types import ModuleType
class VerboseModule(ModuleType):
def __repr__(self):
return... | large_dataset/raw/docs/python/datamodel/datamodel_38.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.