hash, sum, super, idlen, max, min, next, sumdelattr, dir, getattr, globals, locals, setattr, varsascii, format, reprinput, open, print __import__breakpoint, @classmethod, compile, eval, exec, help, @staticmethod
Draft for Information Only
Content
Python Built-in Misc. Functions hash() Parameters Remarks sum() Parameters Remarks super() Parameters Remarks id() Parameters Remarks Source and Reference
Python Built-in Misc. Functions
The Python interpreter has some built-in misc. object functions.
hash()
hash(object)
Parameters
type()to return the hash value of the specified object.
Remarks
- to return the hash value of the
object (if it has one).
- Hash values are integers.
- They are used to quickly compare dictionary keys during a dictionary lookup.
- Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).
- For objects with custom
__hash__() methods, note that hash() truncates the return value based on the bit width of the host machine.
sum()
sum(iterable, /, start=0)
Parameters
sum()to return the sum of the elements of an iterable .
iterableto specify an iterable to be returned from
/
start=0to specify the starting value
Remarks
- Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.
For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling ''.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain().
super()
super([type[, object-or-type]])
Parameters
super()to return a proxy object that delegates method calls to a parent or sibling class of type.
[type]optional, to specify the type to be returned from
[object-or-type]optional, to specify the object-or-type to be searched from
Remarks
- Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.
The object-or-type determines the method resolution order to be searched. The search starts from the class right after the type.
For example, if __mro__ of object-or-type is D -> B -> C -> A -> object and the value of type is B, then super() searches C -> A -> object.
The __mro__ attribute of the object-or-type lists the method resolution search order used by both getattr() and super(). The attribute is dynamic and can change whenever the inheritance hierarchy is updated.
If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).
There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.
The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).
For both use cases, a typical superclass call looks like this:
class C(B):
def method(self, arg):
super().method(arg) # This does the same thing as:
# super(C, self).method(arg)
In addition to method lookups, super() also works for attribute lookups. One possible use case for this is calling descriptors in a parent or sibling class.
Note that super() is implemented as part of the binding process for explicit dotted attribute lookups such as super().__getitem__(name). It does so by implementing its own __getattribute__() method for searching classes in a predictable order that supports cooperative multiple inheritance. Accordingly, super() is undefined for implicit lookups using statements or operators such as super()[name].
Also note that, aside from the zero argument form, super() is not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references. The zero argument form only works inside a class definition, as the compiler fills in the necessary details to correctly retrieve the class being defined, as well as accessing the current instance for ordinary methods.
id()
id(object)
Parameters
id()to return the identity of the specified object.
objectto specify the object to be returned from
Remarks
id() returns an integer representing the specified object.
- The returned integer is guaranteed to be unique and constant during the lifetime of the specified
object
- For CPython,
id()returns the address of the specified object in memory.
Source and Reference
©sideway
ID: 200602902 Last Updated: 6/29/2020 Revision: 0
|
|