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.
...constructor? And if so, does that alternate constructor need to have the same name as the original constructor? Customizing the dict in which the block is executed Should users of the make-statement be able to determine in which dict object the code is executed? This would allow the make-statement to be used in situations where a normal dict object would not suffice, e.g. if order and repeated names must be allowed. Allowing this sort of customization could allow XML to be written without r...
...constructor, it is similar to semantics of functions, but a function takes a value and returns a value, while generic type constructor takes a type and "returns" a type. It is common when a particular class or a function behaves in such a type generic manner. Consider two examples: Container classes, such as list or dict, typically contain only values of a particular type. Therefore, a user might want to type annotate them as such: users = [] # type: List[UserID] users.append(UserID(42)) # OK ...
...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 ...
...constructor is no longer called. This makes a much larger group of classes picklable by default, but may occasionally change semantics. To force calling __init__() on unpickling, define a __getinitargs__() method. Other changes too, in particular cPickle now handles classes defined in packages correctly. The same change applies to copying instances with copy.py. Locale support in the "re" (Perl regular expressions) module. Use the flag re.L (or re.LOCALE) to enable locale-specific...
...declaration creates a class attribute for each variable containing a descriptor, and setting a class attribute to a default value would overwrite this descriptor. There's no check that prevents you to override an instance variable already defined by a base class using a __slots__ declaration. If you do that, the instance variable defined by the base class is inaccessible (except by retrieving its descriptor directly from the base class; this could be used to rename it). Doing this r...
...constructors: the class statement just calls the metatype of the first base (or that specified by the __metaclass__ variable), and this metatype's constructor looks for the most derived metatype. If that is itself, it proceeds; otherwise, it calls that metatype's constructor. (Ultimate flexibility: another metatype might choose to require that all bases have the same metatype, or that there's only one base class, or whatever.) (In [1], a new metaclass is automatically derived that is a subclas...
...constructor). The first argument to __new__ must be a class; the remaining arguments are the arguments as seen by the constructor call. A __new__ method that overrides a base class's __new__ method may call that base class's __new__ method. The first argument to the base class's __new__ method call should be the class argument to the overriding __new__ method, not the base class; if you were to pass in the base class, you would get an instance of the base class. Unless you want to...
...constructor). The first argument to __new__ must be a class; the remaining arguments are the arguments as seen by the constructor call. A __new__ method that overrides a base class's __new__ method may call that base class's __new__ method. The first argument to the base class's __new__ method call should be the class argument to the overriding __new__ method, not the base class; if you were to pass in the base class, you would get an instance of the base class. (This is really jus...
...constructor that allows the construction of a ZoneInfo object from any TZif byte stream. This constructor takes an optional parameter, key, which sets the name of the zone, for the purposes of __str__ and __repr__ (see Representations). Unlike the primary constructor, this always constructs a new object. There are two reasons that this deviates from the primary constructor's caching behavior: stream objects have mutable state and so determining whether two inputs are identical is difficult or ...
...constructor are now a shorthand for creating a dictionary from the key/value pairs, and dict.fromkeys(iterable) returns a dict whose keys are taken from the given iterable (the values default to None). Also a new dict method was added, pop(key), which removes and returns the value corresponding to the given key. filter() - now returns Unicode when the input is Unicode. Various bugs with subclasses of built-in types fixed. (New in 2.3a2.) int() - this can now return a long when converting a str...
...declarations of the following form: class x: def __attr_XXX__(self, op, val ): if op=="get": return someComputedValue(self.internal) elif op=="set": self.internal=someComputedValue(val) elif op=="del": del self.internal Client code looks like this: fooval=x.foo x.foo=fooval+5 del x.foo Semantics Attribute references of all three kinds should call the method. The op parameter can be "get"/"set"/"del". Of course this string will...
...declarations. Functions and classes can use generics as type parameterization. Interfaces can have optional fields. Interfaces can specify array and dictionary types. Classes can have constructors that implicitly add arguments as fields. Classes can have static fields. Classes can have private fields. Classes can have getters/setters for fields (like property). Types are inferred. Example code with types: interface Drivable { start(): void; drive(distance: number): boolean; get...
...declaration. A solution to this problem is to move the transformation of the method closer to the method's own declaration. The intent of the new syntax is to replace def foo(cls): pass foo = synchronized(lock)(foo) foo = classmethod(foo) with an alternative that places the decoration in the function's declaration: @classmethod @synchronized(lock) def foo(cls): pass Modifying classes in this fashion is also possible, though the benefits are not as immediately apparent. Almost certa...
...declarations similar to C structs. This could be useful, for example, for future ctypes releases as well as ORMs that define database tables as classes, like the one the Django framework ships. Django currently uses an ugly hack to restore the ordering of members in database models. The RawConfigParser class accepts a dict_type argument that allows an application to set the type of dictionary used internally. The motivation for this addition was expressly to allow users to provide an ordered d...
...Declaration A parameter specification variable is defined in a similar manner to how a normal type variable is defined with typing.TypeVar. from typing import ParamSpec P = ParamSpec("P") # Accepted P = ParamSpec("WrongName") # Rejected because P =/= WrongName The runtime should accept bounds and covariant and contravariant arguments in the declaration just as typing.TypeVar does, but for now we will defer the standardization of the semantics of those options to a later PEP. Valid us...
...constructor signature: ContextVar(name, *, default=_NO_DEFAULT). The name parameter is used for introspection and debug purposes, and is exposed as a read-only ContextVar.name attribute. The default parameter is optional. Example: # Declare a context variable 'var' with the default value 42. var = ContextVar('var', default=42) (The _NO_DEFAULT is an internal sentinel object used to detect if the default value was provided.) ContextVar.get(default=_NO_DEFAULT) returns a value for the context...
...constructor. For example: @dataclass class C: x: int @classmethod def from_file(cls, filename): with open(filename) as fl: file_value = int(fl.read()) return C(file_value) c = C.from_file('file.txt') Because the __post_init__ function is the last thing called in the generated __init__, having a classmethod constructor (which can also execute code immediately after constructing the object) is functionally equivalent to being able to pass parameters to a...
...constructor signature. However this is not unique to Type[]: class methods have similar concerns. A type checker ought to flag violations of such assumptions, but by default constructor calls that match the constructor signature in the indicated base class (User in the example above) should be allowed. A program containing a complex or extensible class hierarchy might also handle this by using a factory class method. A future revision of this PEP may introduce better ways of dealing with thes...
...constructors or by the str.format method, any decimal digit can be used. For example ߅ (NKO DIGIT FIVE) or ௫ (TAMIL DIGIT FIVE) work as the digit 5. Some scripts include digits that look similar to ASCII ones, but have a different value. For example: >>> int('৪୨') 42 >>> '{٥}'.format('zero', 'one', 'two', 'three', 'four', 'five') five Bidirectional Text Some scripts, such as Hebrew or Arabic, are written right-to-left. Phrases in such scripts interact with nearby text in wa...