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...
...functions. Sometimes, however, patterns arise in the structure of the functions rather than the actual sequence of statements. For example, many functions acquire a lock, execute some code specific to that function, and unconditionally release the lock. Repeating the locking code in every function that uses it is error prone and makes refactoring difficult. Block statements provide a mechanism for encapsulating patterns of structure. Code inside the block statement runs under the control of a...
...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...
...functions that consume their arguments immediately. For more complex applications, full generator definitions are always superior in terms of being obvious about scope, lifetime, and binding [2]. Reduction Functions The utility of generator expressions is greatly enhanced when combined with reduction functions like sum(), min(), and max(). The heapq module in Python 2.4 includes two new reduction functions: nlargest() and nsmallest(). Both work well with generator expressions and keep no mo...
...anonymous functions, e.g. for use as callbacks or (pseudo)-lazy evaluation schemes. Often, lambdas are not used when they would be appropriate, just because the keyword "lambda" makes code look complex. Omitting lambda in some special cases is possible, with small and backwards compatible changes to the grammar, and provides a cheap cure against such "lambdaphobia". Rationale Sometimes people do not use lambdas because they fear to introduce a term with a theory behind it. This proposal mak...
...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...
...Definition lists are written like this: what Definition lists associate a term with a definition. how The term is a one-line phrase, and the definition is one or more paragraphs or body elements, indented relative to the term. Tables Simple tables are easy and compact: ===== ===== ======= A B A and B ===== ===== ======= False False False True False False False True False True True True ===== ===== ======= There must be at least two columns in a ...
...functions and methods small. Thread: "Draft proposal: Implicit self in Python 3.0" https://mail.python.org/pipermail/python-dev/2006-January/059468.html lambda will not be renamed. At one point lambda was slated for removal in Python 3000. Unfortunately no one was able to come up with a better way of providing anonymous functions. And so lambda is here to stay. But it is here to stay as-is. Adding support for statements is a non-starter. It would require allowing multi-line lambda expressi...
...definition syntax and PEP 3129's class decorators -- are still being formalized and may change. Function names will, of course, be subject to lengthy bikeshedding debates. Performing Your Role Static Role Assignment Let's start out by defining Tree and Dog classes class Tree(Vegetable): def bark(self): return self.is_rough() class Dog(Animal): def bark(self): return self.goes_ruff() While both implement a bark() method with the same signature, they do wildly different thin...
...functioning Council of Elders at any particular moment. Council composition The council should not be too big nor too small, probably somewhere between 5 and 10 members. There is no reason to fix this number. The members should be knowledgeable about Python and the Python community, and willing to be impartial while operating as part of the council. Council members may be core developers but this is not a requirement. Everyone in the community should feel represented by the council so it would...
...function arguments by name. Function types can optionally include names, for example: (x: Int, y: String) => Bool Unlike in TypeScript and Kotlin, these names are part of the type if provided - any function implementing the type must use the same names. This is similar to the extended syntax proposal we describe in our Rejected Alternatives section. Function Definitions vs Callable Type Annotations In all of the languages listed above, type annotations for function definitions use a : rat...
...definitions or the initializer structure changed. What I really wanted was code that would analyze the shape and members of the initializer, query the class definitions themselves about their members, and then adjust itself to impedance-match the two sets. This kind of thing is called metaclass hacking and is generally considered fearsomely esoteric -- deep black magic. Most object-oriented languages don't support it at all; in those that do (Perl being one), it tends to be a complicated and fra...
...Functions/methods whose argument is another function (plus its arguments) need some way to determine which arguments are destined for the target function, and which are used to configure how they run the target. This is simple (if non-extensible) for positional parameters, but we need some way to distinguish these for keywords. [4] An indexed notation would afford a Pythonic way to pass keyword arguments to these functions without cluttering the caller's code. >>> # Let's start this ex...
...definition section with typing.Sized: from typing import Sized class SupportsClose(Protocol): def close(self) -> None: ... class SizedAndClosable(Sized, SupportsClose, Protocol): pass The two definitions of SizedAndClosable are equivalent. Subclass relationships between protocols are not meaningful when considering subtyping, since structural compatibility is the criterion, not the MRO. If Protocol is included in the base class list, all the other base classes must be prot...
...function _PyTime_DoubleToTimet added, to convert a Python timestamp (C double) to platform time_t with some out-of-bounds checking. Declared in new header file timefuncs.h. It would be good to expose some other internal timemodule.c functions there. New public functions PyEval_EvaluateFrame and PyGen_New to expose generator objects. New public functions Py_IncRef() and Py_DecRef(), exposing the functionality of the Py_XINCREF() and Py_XDECREF macros. Useful for runtime dynamic embedding of Pyt...
...Function objects now have a __module__ attribute that is bound to the name of the module in which the function was defined. This applies for C functions and methods as well as functions and methods defined in Python. This attribute is used by pickle.whichmodule(), which changes the behavior of whichmodule slightly. In Python 2.2 whichmodule() returns "__main__" for functions that are not defined at the top-level of a module (examples: methods, nested functions). Now whichmodule() wi...
...function to a new builtin function. Alternatively, the ? existence checking symbol could be supported as a modifier on the if and while keywords to indicate the use of an existence check rather than a truth check. However, it isn't at all clear that the potential consistency benefits gained for either suggestion would justify the additional disruption, so they've currently been omitted from the proposal. Defining expected invariant relations between __bool__ and __exists__ The PEP currently le...
...function, such as determining which TLS versions support which ciphers, if that functionality is found to be useful or necessary. If users find this approach to be onerous, a future extension to this API can provide helpers that can reintroduce OpenSSL's aggregation functionality. class CipherSuite(IntEnum): TLS_RSA_WITH_RC4_128_SHA = 0x0005 TLS_RSA_WITH_IDEA_CBC_SHA = 0x0007 TLS_RSA_WITH_3DES_EDE_CBC_SHA = 0x000a TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA = 0x0010 TLS_DHE_RSA_WITH_3D...
...Anonymous Block Statements" of Guido's PEP 340. This PEP aims to take the good parts of PEP 340, blend them with parts of PEP 310 and rearrange the lot into an elegant whole. It borrows from various other PEPs in order to paint a complete picture, and is intended to stand on its own. Author's Note During the discussion of PEP 340, I maintained drafts of this PEP as PEP 3XX on my own website (since I didn't have CVS access to update a submitted PEP fast enough to track the activity on python-...