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.
...types class _MyModuleType(types.ModuleType): @property def prop(self, instance, owner): ... sys.modules[__name__].__class__ = _MyModuleType This works, and is supported behavior, but it's clumsy and obscure. This PEP proposes a per-type opt-in extension to the descriptor protocol specifically designed to enable properties in modules. The mechanism is a way to honor the descriptor protocol for members of instances of a class without the member being declared as a class variable....
...types. For example, you can't directly subclass the dictionary type, and the introspection interface for finding out what methods and instance variables an object has is different for types and for classes. Healing the class/type split is a big effort, because it affects many aspects of how Python is implemented. This PEP concerns itself with making the introspection API for types look the same as that for classes. Other PEPs will propose making classes look more like types, and subclassing f...
...declaration, should preserve the meaning of the program. Python 2.x non-Unicode strings violate this principle; Python 3000 bytes literals shouldn't. A string literal with a b in the prefix is always a syntax error in Python 2.5, so this syntax can be introduced in Python 2.6, along with the bytes type. A bytes literal produces a new object each time it is evaluated, like list displays and unlike string literals. This is necessary because bytes literals, like lists and unlike strings, are mutab...
...type and you're in business. There's a new built-in name, "dict", for the type of dictionaries. (In version 2.2b1 and before, this was called "dictionary"; while in general I don't like abbreviations, "dictionary" was just too long to type, and we've been saying "dict" for years.) This is really just sugar, since there are already two other ways to name this type: type({}) and (after importing the types module) types.DictType (and a third, types.DictionaryType). But now that types p...
...Type1, Type2, Type3] Callable, used as Callable[[Arg1Type, Arg2Type], ReturnType] Tuple, used by listing the element types, for example Tuple[int, int, str]. The empty tuple can be typed as Tuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example Tuple[int, ...]. (The ... here are part of the syntax, a literal ellipsis.) TypeVar, used as X = TypeVar('X', Type1, Type2, Type3) or simply Y = TypeVar('Y') (see above for more details) Generic, used to c...
...declaration as a declaration of "iso-8859-1". This will cause arbitrary byte strings to correctly round-trip between step 2 and step 5 of the processing, and provide compatibility with Python 2.2 for Unicode literals that contain non-ASCII bytes. A warning will be issued if non-ASCII bytes are found in the input, once per improperly encoded input file. Remove the warning, and change the default encoding to "ascii". The builtin compile() API will be enhanced to accept Unicode as input. 8-bit ...
...type object and type(x) is y. repr() and str() of class and instance objects now reflect the package/module in which the class is defined. Module "ni" has been removed. (If you really need it, it's been renamed to "ni1". Let me know if this causes any problems for you. Package authors are encouraged to write __init__.py files that support both ni and 1.5 package support, so the same version can be used with Python 1.4 as well as 1.5.) The thread module is now automatically ...
...type T, the type Literal[v] shall be treated as a subtype of T. For example, Literal[3] is a subtype of int. All methods from the parent type will be directly inherited by the literal type. So, if we have some variable foo of type Literal[3] it’s safe to do things like foo + 5 since foo inherits int’s __add__ method. The resulting type of foo + 5 is int. This "inheriting" behavior is identical to how we handle NewTypes. Equivalence of two Literals Two types Literal[v1] and Literal[v2] are equi...
...declarations to an RPM SPEC file Appendix C: Summary of differences from PEP 345 Metadata-Version semantics Switching to a JSON compatible format Changing the version scheme Source labels Support for optional dependencies for distributions Support for different kinds of semantic dependencies Support for metadata extensions Appendix D: Deferred features Standard extensions Improved handling of project obsolescence, renames and mergers MIME type registration String methods in environment markers...
...type checker. c3 = CustomerModel() c4 = CustomerModel(327, first_name="John") c5 = CustomerModel(327, "John Smith", 0) Decorator function example _T = TypeVar("_T") # The ``create_model`` decorator is defined by a library. # This could be in a type stub or inline. @typing.dataclass_transform() def create_model(cls: Type[_T]) -> Type[_T]: cls.__init__ = ... cls.__eq__ = ... cls.__ne__ = ... return cls # The ``create_model`` decorator can now be used to create new model # c...
...types (except in a limited form for arithmetic operators, where "right-hand" (__r*__) methods can be used to do two-argument dispatch. In addition, it is currently a common anti-pattern for Python code to inspect the types of received arguments, in order to decide what to do with the objects. For example, code may wish to accept either an object of some type, or a sequence of objects of that type. Currently, the "obvious way" to do this is by type inspection, but this is brittle and closed to ...
PEP 316 -- Programming by Contract for Python PEP:316 Title:Programming by Contract for Python Author:Terence Way <terry at wayforward.net> Status:Deferred Type:Standards Track Created:02-May-2003 Post-History: Contents Abstract Motivation Specification Exceptions Inheritance Rationale Reference Implementation References Copyright Abstract This submission describes programming by contract for Python. Eiffel's Design By Contract(tm) is perhaps the most popular use of ...
...TypeError elif name=="readonlyattr2": raise TypeError ... else: self.__dict__["name"]=val This has the following problems: The creator of the method must be intimately aware of whether somewhere else in the class hierarchy __setattr__ has also been trapped for any particular purpose. If so, she must specifically call that method rather than assigning to the dictionary. There are many different reasons to overload __setattr__ so there is a decent potential fo...
PEP 382 -- Namespace Packages PEP:382 Title:Namespace Packages Author:Martin v. Löwis <martin at v.loewis.de> Status:Rejected Type:Standards Track Created:02-Apr-2009 Python-Version:3.2 Post-History: Contents Rejection Notice Abstract Terminology Namespace packages today Rationale Specification Impact on Import Hooks Discussion References Copyright Rejection Notice On the first day of sprints at US PyCon 2012 we had a long and fruitful discussion about PEP 382 and P...
...declaration. Function definition: def name ... Argument declaration: def f(...name...), lambda ...name... Class definition: class name ... Assignment statement: name = ... Import statement: import name, import module as name, from module import name Implicit assignment: names are bound by for statements and except clauses There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an...
...type, ob_refcnt, and ob_size, macros: #define Py_TYPE(o) (((PyObject*)(o))->ob_type) #define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt) #define Py_SIZE(o) (((PyVarObject*)(o))->ob_size) are added. E.g. the code blocks #define PyList_CheckExact(op) ((op)->ob_type == &PyList_Type) return func->ob_type->tp_name; needs to be changed to: #define PyList_CheckExact(op) (Py_TYPE(op) == &PyList_Type) return Py_TYPE(func)->tp_name; For initialization of type obj...
...declaration within parentheses. A version declaration is a series of conditional operators and version numbers, separated by commas. Conditional operators must be one of "<", ">", "<=", ">=", "==", and "!=". Version numbers must be in the format accepted by the distutils.version.StrictVersion class: two or three dot-separated numeric components, with an optional "pre-release" tag on the end consisting of the letter 'a' or 'b' followed by a number. Example version numbers are "1.0"...