Notice: While JavaScript is not essential for this website, your interaction with the content will be limited. Please turn JavaScript on for the full experience.
...immutable mapping is desirable: Immutable mappings are hashable which allows their use as dictionary keys or set elements. This hashable property permits functions decorated with @functools.lru_cache() to accept immutable mappings as arguments. Unlike an immutable mapping, passing a plain dict to such a function results in error. Immutable mappings can hold complex state. Since immutable mappings can be copied by reference, transactional mutation of state can be efficiently impleme...
...immutable mapping is desirable: Immutable mappings are hashable which allows their use as dictionary keys or set elements. This hashable property permits functions decorated with @functools.lru_cache() to accept immutable mappings as arguments. Unlike an immutable mapping, passing a plain dict to such a function results in error. Immutable mappings can hold complex state. Since immutable mappings can be copied by reference, transactional mutation of state can be efficiently implemented. Immut...
...immutable(self, *args, **kws): raise TypeError('object is immutable') __setitem__ = _immutable __delitem__ = _immutable clear = _immutable update = _immutable setdefault = _immutable pop = _immutable popitem = _immutable class xdict(dict): def __freeze__(self): return imdict(self) >>> s = set([1, 2, 3]) >>> {s: 4} Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: set obje...
...immutable bytes. Gregory P. Smith proposed a patch that would allow making a bytes object temporarily immutable by requesting that the data be locked using the new buffer API from PEP 3118. This did not seem the right approach to me. Jeffrey Yasskin, with the help of Adam Hupp, then prepared a patch to make the bytes type immutable (by crudely removing all mutating APIs) and fix the fall-out in the test suite. This showed that there aren't all that many places that depend on the mutability of...
...immutable objects can be truly immutable minimize performance penalty for normal Python use cases be careful when immortalizing objects that we don't actually expect to persist until runtime finalization. be careful when immortalizing objects that are not otherwise immutable __del__ and weakrefs must continue working properly Regarding "truly" immutable objects, this PEP doesn't impact the effective immutability of any objects, other than the per-object runtime state (e.g. refcount). So whethe...
...ImmutableSet. For Python 2.4, the new built-in types were named set and frozenset which are slightly less cumbersome. There are two classes implemented in the "sets" module. Instances of the Set class can be modified by the addition or removal of elements, and the ImmutableSet class is "frozen", with an unchangeable collection of elements. Therefore, an ImmutableSet may be used as a dictionary key or as a set element, but cannot be updated. Both types of set require that their elements are i...
...ImmutableDict. It is hashable if keys and values are hashable. werkzeug project has the same code: werkzeug.datastructures.ImmutableDict. ImmutableDict is used for global constant (configuration options). The Flask project uses ImmutableDict of werkzeug for its default configuration. SQLAlchemy project: sqlalchemy.util.immutabledict. It is not hashable and has an extra method: union(). immutabledict is used for the default value of parameter of some functions expecting a mapping. Example: mapper...
...immutable builtin type. The right hand sides need not be all of the same type, but they should be comparable to the type of the left hand switch variable. The compiler could then setup a read-only (perfect) hash table, store it in the constants and add an opcode SWITCH in front of the standard if-elif-else byte code stream which triggers the following run-time behaviour: At runtime, SWITCH would check x for being one of the well-known immutable types (strings, unicode, numbers) and use the hash...
...immutable, often const treated as immutable related to CPython's main() execution related to the REPL set during runtime init, then treated as immutable mutable, protected by some global lock mutable, atomic Note that currently the allocators (see Objects/obmalloc.c) are shared between all interpreters, protected by the GIL. They will need to move to each interpreter (or a global lock will be needed). This is the highest risk part of the work to isolate interpreters and may require more than ...
...immutable, which makes them hashable and also prevents a class of programming errors. Sane behaviour Little of the functionality from os.path is reused. Many os.path functions are tied by backwards compatibility to confusing or plain wrong behaviour (for example, the fact that os.path.abspath() simplifies ".." path components without resolving symlinks first). Comparisons Paths of the same flavour are comparable and orderable, whether pure or not: >>> PurePosixPath('a') == PurePos...
...immutable version of the distribution. The immutability is guaranteed by package indexes such as Warehouse. The package index to use is typically known from environmental or command line parameters of the installer. This freeze mechanism therefore works fine for installation mode 1 (i.e. when the package to install was specified as a name plus version specifier). For installation mode 2, i.e. when the package to install was specified as a direct URL reference, the name==version tuple is obviousl...
...immutable type. If instead of __new__ we had tried to override __init__, it wouldn't have worked: >>> class inch(float): ... "THIS DOESN'T WORK!!!" ... def __init__(self, arg=0.0): ... float.__init__(self, arg*0.0254) ... >>> print inch(12) 12.0 >>> The version overriding __init__ doesn't work because the float type's __init__ is a no-op: it returns immediately, ignoring its arguments. All this is done so that immutable ...
...immutable type. If instead of __new__ we had tried to override __init__, it wouldn't have worked: >>> class inch(float): ... "THIS DOESN'T WORK!!!" ... def __init__(self, arg=0.0): ... float.__init__(self, arg*0.0254) ... >>> print inch(12) 12.0 >>> The version overriding __init__ doesn't work because the float type's __init__ is a no-op: it returns immediately, ignoring its arguments. All this is done so that immutable types can preserve their immuta...
...immutable? (Guido seems to like them to be mutable.) Copyright This document has been placed in the public domain. Source: https://github.com/python/peps/blob/master/pep-0332.txt
...immutable list of immutable mappings as a fundamental data structure is motivated by the need to efficiently implement contextvars.get_execution_context(), which is to be frequently used by asynchronous tasks and callbacks. When the EC is immutable, get_execution_context() can simply copy the current execution context by reference: def get_execution_context(self): return PyThreadState_Get().ec Let's review all possible context modification scenarios: The ContextVariable.set() method is c...
...immutable. Immutable ABCs and containers may be used in combination with Final to prevent mutating such values: x: Final = ['a', 'b'] x.append('c') # OK y: Final[Sequence[str]] = ['a', 'b'] y.append('x') # Error: "Sequence[str]" has no attribute "append" z: Final = ('a', 'b') # Also works Type checkers should treat uses of a final name that was initialized with a literal as if it was replaced by the literal. For example, the following should be allowed: from typing import NamedTuple, Fina...
...immutable, so as not degrade performance by much. They will need to be immutable to guarantee that the AST remains a tree to avoid having to support cyclic GC. Making them immutable means they will not have a __dict__ attribute, making them compact. AST nodes in the ast module will remain mutable. Currently, all AST nodes are allocated using an arena allocator. Changing to use the standard allocator might slow compilation down a little, but has advantages in terms of maintenance, as much code ca...
...immutable at the Python level: for example, you can't set str.myattribute = 123. Note Sharing truly immutable objects between interpreters is fine, as long as they don't provide access to mutable objects. But, every Python object has a mutable implementation detail: the reference count. Changes to the refcount are guarded by the GIL. Thus, code that shares any Python objects across interpreters implicitly depends on CPython's current, process-wide GIL. Because they are immutable and process-gl...