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.
...namespace will leave __qualname__ untouched, so the implementation location remains readily accessible for introspection. If __name__ is adjusted at the top of a module, then this will automatically adjust the __module__ attribute for all functions and classes subsequently defined in that module. Since multiple submodules may be set to use the same "public" namespace, functions and classes will be given a new __qualmodule__ attribute that refers to the __qualname__ of their module. This isn't st...
...namespace, this function returns the same namespace as globals(). At class scope, it returns the namespace that will be passed to the metaclass constructor. When using exec() or eval() with separate local and global namespaces, it returns the local namespace passed in to the function call. In all of the above cases, each call to locals() in a given frame of execution will return the same mapping object. Changes made through the mapping object returned from locals() will be visible as bound, rebo...
...definition is an executable statement. It first evaluates the extends list, if present. Each item in the extends list should evaluate to an interface object. The interface's suite is then executed in a new execution frame (see the Python Reference Manual, section 4.1), using a newly created local namespace and the original global namespace. When the interface's suite finishes execution, its execution frame is discarded but its local namespace is saved as interface elements. An interface objec...
...namespace. The optional dictionary argument init_globals may be used to pre-populate the globals dictionary before the code is executed. The supplied dictionary will not be modified. If any of the special global variables below are defined in the supplied dictionary, those definitions are overridden by the run_module function. The special global variables __name__, __file__, __loader__ and __builtins__ are set in the globals dictionary before the module code is executed. __name__ is set to run_n...
...namespace. This suggests there may be a broader compatibility issue where some scripts are relying on direct execution and import giving different namespaces (just as package execution keeps the two separate by executing the __main__ submodule in the __main__ namespace, while the package name references the __init__ file as usual. Background I tripped over this issue while debugging a main program via a module which tried to monkey patch a named module, that being the main program module. Na...
...definitions cannot be nested. But in Python, though functions are usually defined at the top level, a function definition can be executed anywhere. This gave Python the syntactic appearance of nested scoping without the semantics, and yielded inconsistencies that were surprising to some programmers -- for example, a recursive function that worked at the top level would cease to work when moved inside another function, because the recursive function's own name would no longer be visible in its ...
...namespace in which to put future import-related information and to pass around within the import system. Secondly, there's an API void between finders and loaders that causes undue complexity when encountered. The PEP 420 (namespace packages) implementation had to work around this. The complexity surfaced again during recent efforts on a separate proposal. [1] The finder and loader sections above detail current responsibility of both. Notably, loaders are not required to provide any of the f...
...definition in the nearest enclosing function scope will be used. One consequence of this change is that lambda statements could reference variables in the namespaces where the lambda is defined. In some unusual cases, this change will break code. In all previous version of Python, names were resolved in exactly three namespaces -- the local namespace, the global namespace, and the builtin namespace. According to this old definition, if a function A is defined within a function B, the names bo...
...namespace. This also means that names imported in this fashion are always local, and do not follow the global directive. An additional change to this syntax has also been suggested, to generalize the expression given after the as clause. Rather than a single name, it could be allowed to be any expression that yields a valid l-value; anything that can be assigned to. The change to accommodate this is minimal, as the patch [2] proves, and the resulting generalization allows a number of new const...
...namespaces. The value is converted with str() if the containing string is an 8-bit string, or with unicode() if it is a Unicode string. A Python identifier optionally followed by any number of trailers, where a trailer consists of: - a dot and an identifier, - an expression enclosed in square brackets, or - an argument list enclosed in parentheses (This is exactly the pattern expressed in the Python grammar by "NAME trailer*", using the definitions in Grammar/Grammar.) Any complete Python expr...
...namespace where user code is entered and executed. If that namespace is __main__ (as it is for the default CPython REPL), then no changes are needed beyond those in this PEP. Interactive shell implementations which use a namespace other than __main__ will need to add their own filter. For example, IPython uses the following command ([8]) to set up a suitable filter: warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name...
...definition above will bind flatten to whatever it was previously bound to. The second will do the same, if it was already bound to the when decorator's first argument. If flatten is unbound or bound to something else, it will be rebound to the function definition as given. The last two definitions above will always bind flatten_basestring to the function definition as given. Using this approach allows you to both give a method a descriptive name (often useful in tracebacks!) and to reuse the ...
...namespace for names that are part of the Python language definition, so that programmers are free to create variables, attributes, and methods that start with letters, without fear of silently colliding with names that have a language-defined purpose. (Colliding with reserved keywords is still a concern, but at least this will immediately yield a syntax error.) The naming of the next method on iterators is an exception to this convention. Code that nowhere contains an explicit call to a next m...
...definition syntax, you are free to use inheritance, metaclasses, docstrings, user-defined methods, class factories, and other Python class features. A class decorator is provided which inspects a class definition for variables with type annotations as defined in PEP 526, "Syntax for Variable Annotations". In this document, such variables are called fields. Using these fields, the decorator adds generated method definitions to the class to support instance initialization, a repr, comparison met...
...definition Legacy manylinux tags Package installers Package indexes Rejected alternatives Abstract This PEP proposes a scheme for new 'manylinux' wheel tags to be defined without requiring a PEP for every specific tag, similar to how Windows and macOS tags already work. This will allow package maintainers to take advantage of new tags more quickly, while making better use of limited volunteer time. Non-goals include: handling non-glibc-based platforms; integrating with external package mana...
...Definition Language (ASDL) [Wang97]. The definition of the AST nodes for Python is found in the file Parser/Python.asdl . Each AST node (representing statements, expressions, and several specialized types, like list comprehensions and exception handlers) is defined by the ASDL. Most definitions in the AST correspond to a particular source construct, such as an 'if' statement or an attribute lookup. The definition is independent of its realization in any particular programming language. The fol...
...definition time. Instead, they are preserved in __annotations__ in string form. This change is being introduced gradually, starting with a __future__ import in Python 3.7. Rationale and Goals PEP 3107 added support for arbitrary annotations on parts of a function definition. Just like default values, annotations are evaluated at function definition time. This creates a number of issues for the type hinting use case: forward references: when a type hint contains names that have not been defi...
...namespace of the class body which are only valid during class construction. An example of this might be "field constructors", small functions that are used in the creation of class members. Another interesting possibility is supporting forward references, i.e. references to Python symbols that are declared further down in the class body. The other, weaker, rationale is purely cosmetic: The current method for specifying a metaclass is by assignment to the special variable __metaclass__, which is ...
...namespace lookups (local, global, builtin). The "role" of the interpreted text (identifying a class, module, function, etc.) is determined implicitly from the namespace lookup. For example: class Keeper(Storer): """ Keep data fresher longer. Extend `Storer`. Class attribute `instances` keeps track of the number of `Keeper` objects instantiated. """ instances = 0 """How many `Keeper` objects are there?""" def __init__(self): """ Extend `Stor...
...definition as well. Option 4 There are a number of proposals to add a construct to the language that makes the concept of a value pre-computed at function definition time generally available, without tying it either to parameter default values or case expressions. Some keywords proposed include 'const', 'static', 'only' or 'cached'. The associated syntax and semantics vary. These proposals are out of scope for this PEP, except to suggest that if such a proposal is accepted, there are two way...