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.
...function or class definition, those scopes will see the trailing function or class definition rather than any other bindings for that name in the containing scope. In a very real sense, this proposal is about making it possible to override the implicit "name = <defined function or class>" name binding operation that is part of every function or class definition, specifically in those cases where the local name binding isn't actually needed. Under this PEP, an ordinary class or function def...
...function specification block starts with a /*[define token, followed by an optional C function name, followed by a right bracket. If the C function name is not given, it is generated from the declaration name. In the example, omitting the name posix_stat would result in a C function name of os_stat. Declaration The required declaration is (almost) a valid Python function definition. The 'def' keyword and the function body are redundant, but the author of this PEP finds the definition more read...
...function that contains a nested function that has free variables. For example, if function F contains a function G and G uses the builtin len(), then F is a function that contains a nested function (G) with a free variable (len). The label "free-in-nested" will be used to describe these functions. import * used in function scope The language reference specifies that import * may only occur in a module scope. (Sec. 6.11) The implementation of C Python has supported import * at the function s...
...definitions of it on the same platform (depending on whether it uses narrow or wide code units). Applications that need to access the contents of a Unicode string can convert it to wchar_t. Functions and function-like Macros By default, all functions are available, unless they are excluded below. Whether a function is documented or not does not matter. Function-like macros (in particular, field access macros) remain available to applications, but get replaced by function calls (unless their de...
...function definition would look like: def name(positional_or_keyword_parameters, *, keyword_only_parameters): Building on that example, the new syntax for function definitions would look like: def name(positional_only_parameters, /, positional_or_keyword_parameters, *, keyword_only_parameters): The following would apply: All parameters left of the / are treated as positional-only. If / is not specified in the function definition, that function does not accept any positional-only arg...
...function definitions A new keyword codef is introduced which is used in place of def to define a cofunction. A cofunction is a special kind of generator having the following characteristics: A cofunction is always a generator, even if it does not contain any yield or yield from expressions. A cofunction cannot be called the same way as an ordinary function. An exception is raised if an ordinary call to a cofunction is attempted. Cocalls Calls from one cofunction to another are made by marki...
...function or method declaration. Motivation The current method of applying a transformation to a function or method places the actual transformation after the function body. For large functions this separates a key component of the function's behavior from the definition of the rest of the function's external interface. For example: def foo(self): perform method operation foo = classmethod(foo) This becomes less readable with longer methods. It also seems less than pythonic to name the...
...Function definitions only allow simple function names to be used, even though functions are assignable first class objects. This PEP proposes adding support for assigning a function to a class or instance attribute directly in the function definition's header by using the dot notation to separate the object from the function's name. Although a similar feature, this PEP does not address general assignment to anything that supports assignment, such as dict keys and list indexes. Rationale Curren...
...function(user_function, tuple=tuple, sorted=sorted, len=len, KeyError=KeyError): ... # 60 line function return decorating_function # Becomes: return ?.decorating_function given: # Cell variables rather than locals, but should give similar speedup tuple, sorted, len, KeyError = tuple, sorted, len, KeyError def decorating_function(user_function): ... # 60 line function # This example also nicely makes it clear that there is nothing in the # function after the nested fu...
...function annotations and variable annotations so that they are no longer evaluated at function 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...
...function definition, class definition, or method definition, after any comments. This is the standard for Python __doc__ attributes. Immediately following a simple assignment at the top level of a module, class definition, or __init__ method definition, after any comments. See Attribute Docstrings below. Additional string literals found immediately after the docstrings in (a) and (b) will be recognized, extracted, and concatenated. See Additional Docstrings below. @@@ 2.2-style "properties" w...
...FunctionDef; struct { expr_ty value; } Return; struct { expr_ty value; } Yield; } v; int lineno; } Also generated are a series of constructor functions that allocate (in this case) a stmt_ty struct with the appropriate initialization. The 'kind' field specifies which component of the union is initialized. The FunctionDef() constructor function sets 'kind' to FunctionDef_kind and ...
...function and is called directly to create the class object. In this case, the final metaclass will be determined by the factory function definition. In the typical case (where the factory functions just calls type, or, in Python 3.3 or later, types.new_class) the actual metaclass is then determined based on the parent classes. It is notable that only the actual metaclass is inherited - a factory function used as a metaclass hook sees only the class currently being defined, and is not invoked fo...
...functions, structure definitions, and major sections inside functions. Comments go before the code they describe. All functions and global variables should be declared static unless they are to be part of a published interface For external functions and variables, we always have a declaration in an appropriate header file in the "Include" directory, which uses the PyAPI_FUNC() macro and PyAPI_DATA() macro, like this: PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); PyAPI_DATA(PyTypeObject)...
...definitions must be followed by exactly one non-@overload-decorated definition (for the same function/method). The @overload-decorated definitions are for the benefit of the type checker only, since they will be overwritten by the non-@overload-decorated definition, while the latter is used at runtime but should be ignored by a type checker. At runtime, calling a @overload-decorated function directly will raise NotImplementedError. Here's an example of a non-stub overload that can't easily be ...
...function expects and returns ([16]) Function overloading / generic functions ([21]) Foreign-language bridges ([17], [18]) Adaptation ([20], [19]) Predicate logic functions Database query mapping RPC parameter marshaling ([22]) Other information Documentation for parameters and return values ([23]) Standard Library pydoc and inspect The pydoc module should display the function annotations when displaying help for a function. The inspect module should change to support annotations. Rel...
...function). Nothing. All CT objects should have been disposed of in step 4 (as a side-effect of clearing references); this collection is finished. Note The GC doesn't recalculate the CI after step 2 above, hence the need for step 3 to check that the whole subgraph is still isolated. C-level changes Type objects get a new tp_finalize slot to which __del__ methods are mapped (and reciprocally). Generators are modified to use this slot, rather than tp_del. A tp_finalize function is a normal...
...function object, the current implementation simply early-binds the function object. Note that, since the annotation function inner.__co_annotations__() is defined while parsing outer(), from Python's perspective the annotation function is a "nested function". So "local variable inside the 'current' function" and "free variable from an enclosing function" are, from the perspective of the annotation function, the same thing. Annotations That Refer To Class Variables It's possible to write annot...
...function argument in a special kind of descriptor whose __get__() method returns the original function unchanged. More on __get__ methods: in Python 2.2, the magic of binding methods to instances (even for classic classes!) is done through the __get__ method of the object found in the class. The __get__ method for regular function objects returns a bound method object; the __get__ method for staticfunction objects returns the underlying function. If a class attribute has no __get__ m...