Why is a function/method call in python expensive?

A function call requires that the current execution frame is suspended, and a new frame is created and pushed on the stack. This is relatively expensive, compared to many other operations. You can measure the exact time required with the timeit module: >>> import timeit >>> def f(): pass … >>> timeit.timeit(f) 0.15175890922546387 That’s 1/6th … Read more

Python frozenset hashing algorithm / implementation

The problem being solved is that the previous hash algorithm in Lib/sets.py had horrendous performance on datasets that arise in a number of graph algorithms (where nodes are represented as frozensets): # Old-algorithm with bad performance def _compute_hash(self): result = 0 for elt in self: result ^= hash(elt) return result def __hash__(self): if self._hashcode is … Read more

What are __signature__ and __text_signature__ used for in Python 3.4

These attributes are there to enable introspection for Python objects defined in C code. The C-API Argument Clinic provides the data, to assist the inspect module when building Signature objects. Introspection of C-API functions was not supported before. See the internal inspect._signature_fromstr() function on how the __text_signature__ value is used. Currently, the __text_signature__ attribute is … Read more