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 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" section is the function declaration. Indented lines below the function declaration declare parameters, one per line; lines below those that are indented even further are per-parameter docstrings. Finally, the first line dedented back to column 0 end parameter declarations and start the function docstring. Parameter docstrings are optional; function docstrings are not. Functions that specify no arguments may simply specify the function declaration followed by the docstring. Module a...
...function scope, augmented assignments will be changed to require that there be either a preceding name binding or variable declaration to explicitly establish the target name as being local to the function, or else an explicit global or nonlocal declaration. TargetNameError, a new SyntaxError subclass, will be raised at compile time if no such binding or declaration is present. For example, the following code would compile and run as it does today: x = 0 x += 1 # Sets global "x" to 1 class C: ...
...function 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 visibl...
...declarations add expressiveness, while at the same time they make the code look much more professional. Compatibility issues To enable type declaration mode, one has to write: from __future__ import type_declarations which enables Unicode parsing of the source [4], makes typedef a keyword and enforces correct types for all assignments and function calls. Rejection After careful considering, much soul-searching, gnashing of teeth and rending of garments, it has been decided to reject this P...
...functions designated initializers (especially nice for type declarations) intermingled declarations booleans C++-style line comments Python versions before 3.6 used ANSI/ISO standard C (the 1989 version of the standard). This meant (amongst many other things) that all declarations must be at the top of a block (not necessarily at the top of function). Don't use compiler-specific extensions, such as those of GCC or MSVC (e.g. don't write multi-line strings without trailing backslashes). All fu...
...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...
...declaration of C structs; COM objects; Automatic translation of Python classes into IDL or database schemas, such as used in an ORM; and so on. In such cases, it would be useful for a Python programmer to specify such ordering directly using the declaration order of class members. Currently, such orderings must be specified explicitly, using some other mechanism (see the ctypes module for an example.) Unfortunately, the current method for declaring a metaclass does not allow for this, since the ...
...declaration itself is made. This PEP introduces new syntax for transformations of a 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 bec...
...function, the function's pre-conditions are checked. An assertion error is raised if any pre-condition is false. If the function is public, then the class or module's invariants are also checked. Copies of variables declared in the post are saved, the function is called, and if the function exits without raising an exception, the post-conditions are checked. Exceptions Class/module invariants are checked even if a function or method exits by signalling an exception (post-conditions are not)....
...declaration rules create confusion when type aliases involve forward references, invalid types, or violate other restrictions enforced on type alias declaration. Because the distinction between an unannotated value and a type alias is implicit, ambiguous or incorrect type alias declarations implicitly default to a valid value assignment. This creates expressions that are impossible to express as type aliases and punts error diagnosis of malformed type aliases downstream. The following examples e...
...function from first_type is also in the set of functions of second_type. The relation defined thus is called a subtype relation. By this definition: Every type is a subtype of itself. The set of values becomes smaller in the process of subtyping, while the set of functions becomes larger. An intuitive example: Every Dog is an Animal, also Dog has more functions, for example it can bark, therefore Dog is a subtype of Animal. Conversely, Animal is not a subtype of Dog. A more formal example: I...
...function, is an instance of the Python idiom of one function passing all arguments given to it over to another function. This is done through the combination of the *args and **kwargs features in both parameters and in arguments. When one defines a function (like inner) that takes (*args, **kwargs) and goes on to call another function with (*args, **kwargs), the wrapping function can only be safely called in all of the ways that the wrapped function could be safely called. To type this decorato...
...function for its instances types can be subtyped in C types can be subtyped in Python with the class statement multiple inheritance from types is supported (insofar as practical -- you still can't multiply inherit from list and dictionary) the standard coercion functions (int, tuple, str etc.) will be redefined to be the corresponding type objects, which serve as their own factory functions a class statement can contain a __metaclass__ declaration, specifying the metaclass to be used to create 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 definition only refers to features of the ABI, such as the various _Check macros) ABI function declarations will not change their parameters or return types. If a change to the signature becomes...
...functionality is provided, so the old interface is deprecated. In other cases, the need for having the functionality of the module may not exist anymore. If the rationale is faulty, again a change to this PEP's text MUST be submitted. This change MUST include the date of undeprecation and a rationale for undeprecation. Modules that are undeprecated under this procedure MUST be listed in this PEP for at least one major release of Python. Obsolete modules A number of modules are already liste...
...function with arguments of Literal type (PEP 586)). Specification The final decorator The typing.final decorator is used to restrict the use of inheritance and overriding. A type checker should prohibit any class decorated with @final from being subclassed and any method decorated with @final from being overridden in a subclass. The method decorator version may be used with all of instance methods, class methods, static methods, and properties. For example: from typing import final @final...
...functions exposed by PyModule_Type to inspect the attribute interacted with, and if it supports the descriptor protocol, call the relevant exposed function. Our implementation faces two challenges: Since this code will be run every time an attribute is looked up on a method, it needs to add very little overhead in the general case, where the object stored in the attribute is not a descriptor. Since functions are descriptors, we must take care to not honor the descriptor protocol for all objects...
...functions. With the make-statement, code that looks something like: root = Tkinter.Tk() frame = Tkinter.Frame(root) frame.pack() def say_hi(): print "hi there, everyone!" hi_there = Tkinter.Button(frame, text="Hello", command=say_hi) hi_there.pack(side=Tkinter.LEFT) root.mainloop() could be rewritten to group the Button's function with its declaration: root = Tkinter.Tk() frame = Tkinter.Frame(root) frame.pack() make Tkinter.Button hi_there(frame): text = "Hello" def command(): ...
...function pointer, a setter C function pointer, and a context pointer. The function pointers are optional, so that for example setting the setter function pointer to NULL makes a read-only attribute. The context pointer is intended to pass auxiliary information to generic getter/setter functions, but I haven't found a need for this yet. Note that there is also a similar mechanism to declare built-in methods: these are PyMethodDef structures, which contain a name and a C function pointer (and so...