When is a function in a standard library module called a built-in function?

There are two meanings of “built-in” here, although they both mean “part of the interpreter”. The library reference uses it to indicate that a function is available without an import (it is “not part of a module”, although see builtins). The interpreter itself uses it to indicate that a function is implemented natively rather than … Read more

Why is str.strip() so much faster than str.strip(‘ ‘)?

In a tl;dr fashion: This is because two functions exist for the two different cases, as can be seen in unicode_strip; do_strip and _PyUnicodeXStrip the first executing much faster than the second. Function do_strip is for the common case str.strip() where no arguments exist and do_argstrip (which wraps _PyUnicode_XStrip) for the case where str.strip(arg) is … Read more

Why is repr(int) faster than str(int)?

Because using str(obj) must first go through type.__call__ then str.__new__ (create a new string) then PyObject_Str (make a string out of the object) which invokes int.__str__ and, finally, uses the function you linked. repr(obj), which corresponds to builtin_repr, directly calls PyObject_Repr (get the object repr) which then calls int.__repr__ which uses the same function as … Read more

Understanding memory allocation for large integers in Python

Why 28 bytes initially for any value as low as 1? I believe @bgusach answered that completely; Python uses C structs to represent objects in the Python world, any objects including ints: struct _longobject { PyObject_VAR_HEAD digit ob_digit[1]; }; PyObject_VAR_HEAD is a macro that when expanded adds another field in the struct (field PyVarObject which … Read more

Two integers in Python have same id, but not lists or tuples

Immutable objects don’t have the same id, and as a matter of fact this is not true for any type of objects that you define separately. Generally speaking, every time you define an object in Python, you’ll create a new object with a new identity. However, for the sake of optimization (mostly) there are some … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)