operator
module?The operator
module is a built-in module in Python. The module provides functions equivalent to the intrinsic operators of Python. For instance, we can use the operator.mul()
method for multiplication instead of using the multiplication (*
) operator.
The module’s functions come in handy when callable methods are given as arguments to another Python object.
length_hint()
methodThe length_hint()
method returns an estimate of the number of elements in the given object. The return value of the method will be exact if the given object supports the len()
method. Otherwise, the method may overestimate or underestimate the number of elements in the object.
import operatorobj = [1, 2, 3]print("operator.length_hint(%s) = %s" % (obj, operator.length_hint(obj)))obj = "hello educative"print("operator.length_hint(%s) = %s" % (obj, operator.length_hint(obj)))obj = 13print("operator.length_hint(%s) = %s" % (obj, operator.length_hint(obj)))
operator
module.operator.length_hint()
method on a list and print the result.operator.length_hint()
method on a string and print the result.operator.length_hint()
method on an integer and print the result.