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 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...
...FunctionType: return value return BoundMethod(value, self) class BoundMethod: def __init__(self, function, instance): self.function = function self.instance = instance def __call__(self, *args): print "calling", self.function, "for", self.instance, "with", args return apply(self.function, (self.instance,) + args) Trace = Tracing('Trace', (), {}) class MyTracedClass(Trace): def method1(self, a): self.a = a ...
...definition of the save method would mean that a D instance, when requested to save itself, only saves the A and B parts of its data, but not the part of its data defined by class C! C++ notices that D inherits two conflicting definitions of method m, and issues an error message. The author of D is then supposed to override m to resolve the conflict. But what is D's definition of m supposed to do? It can call B's m followed by C's m, but because both definitions call the definition of...
...function definition indicates that the interpreter should collect all keyword arguments that do not correspond to other named parameters. However, Python does not preserved the order in which those collected keyword arguments were passed to the function. In some contexts the order matters. This PEP dictates that the collected keyword arguments be exposed in the function body as an ordered mapping. Motivation Python's **kwargs syntax in function definitions provides a powerful means of dynam...
...function is invoked to return the next token. Token numbers identify tokens. In addition, the yylval variable carries the semantic value. Older versions of BisonGen generate flex files from token definitions given XML; the recent versions generate re-style regular expressions from similar XML specifications. Error handling also follows the YACC tradition: an yyerror function is invoked. Since unwinding out of a bison parser run is not easy, this function normally only sets a global...
...Definition list ends without a blank line; unexpected unindent. </pre> In this example, the use of <font color="red">str</font> inside the inner function g() now refers to the argument <font color="red">str</font> in the outer function f(); previously (without nested scopes), it would refer to the built-in function <font color="blue">str</font>. <p><li>Unbound method objects have their im_class field set differen...
...functions are opt-out: all functions not specially marked are part of the Stable ABI. In practice, for Windows there's a list that's opt-in. For users there is a #define that should make only the Stable ABI available, but there is no process that ensures it is kept up-to date. Neither is there a process for updating the documentation. Until recently, the Stable ABI was not tested at all. It tends to break. For example, changing a function to a macro can break the Stable ABI as the function symbo...
...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 definition is encountered. A downside however is that the dispatch dict for a switch inside a nested function must be recomputed each time the nested function is defined. For certain "functional" styles of programming this may make switch unattractive in nested functions. (Unless all case expressions are compile-time constants; then the compiler is of course free to optimize away the switch freezing code and make the dispatch table part of the code object.) Another downside is that un...
...Definition Constants Functions Example Implementation References Copyright Rejection Notice This PEP has been rejected. After sitting open for four years, it has failed to generate sufficient community interest. Several ideas of this PEP were implemented for Python 2.6. float('inf') and repr(float('inf')) are now guaranteed to work on every supported platform with IEEE 754 semantics. However the eval(repr(float('inf'))) roundtrip is still not supported unless you define inf and nan yourself...
...functionality (perhaps using Java or C#). Rationale and Goals Python has always provided a variety of built-in and standard-library generic functions, such as len(), iter(), pprint.pprint(), and most of the functions in the operator module. However, it currently: does not have a simple or straightforward way for developers to create new generic functions, does not have a standard way for methods to be added to existing generic functions (i.e., some are added using registration functions, oth...
...function or method that adds a new feature in one dimension (where the output goes) but takes away in another dimension (spaces between items, and the choice of trailing newline or not). We could add a whole slew of methods or functions to deal with the various cases but that seems to add more confusion than necessary, and would only make sense if we were to deprecate the print statement altogether. I feel that this debate is really about whether print should have been a function or method rath...
...function that behaves in exactly the same fashion. next(x) --> internal tp_iternext --> x.__next__() Further, it is proposed that the next built-in function accept a sentinel value as an optional second argument, following the style of the getattr and iter built-in functions. When called with two arguments, next catches the StopIteration exception and returns the sentinel value instead of propagating the exception. This creates a nice duality between iter and next: iter(function, sent...
...function, a module may implement a number of additional callback functions, which are invoked when the module's tp_traverse, tp_clear, and tp_free functions are invoked, and when the module is reloaded. The entire module definition is combined in a struct PyModuleDef: struct PyModuleDef{ PyModuleDef_Base m_base; /* To be filled out by the interpreter */ Py_ssize_t m_size; /* Size of per-module data */ PyMethodDef *m_methods; inquiry m_reload; traverseproc m_traverse; inquiry m_clea...
...function object. It is not possible to set attributes on bound or unbound methods, except by doing so explicitly on the underlying function object. See the Future Directions discussion below for approaches in subsequent versions of Python. A function object's __dict__ can also be set, but only to a dictionary object. Deleting a function's __dict__, or setting it to anything other than a concrete dictionary object results in a TypeError. If no function attributes have ever been set, the funct...
...definition, a function definition or as first string literal in a module. The string literals are added to the objects in question under the __doc__ attribute and are from then on available for introspection tools which can extract the contained information for help, debugging and documentation purposes. Docstrings appearing in locations other than the ones mentioned are simply ignored and don't result in any code generation. Here is an example: class C: "class C doc-string" a = 1 ...
...functions, have a string attribute called __doc__. If the first expression inside the definition is a literal string, that string is assigned to the __doc__ attribute. The __doc__ attribute is called a documentation string, or docstring. It is often used to summarize the interface of the module, class or function. However, since there is no common format for documentation string, tools for extracting docstrings and transforming those into documentation in a standard format (e.g., DocBook) have n...