python-internals

How does Python interpreter work in dynamic typing?

How does Python interpreter work in dynamic typing? Question: I read this question, but it didn’t give me a clear answer: How does Python interpreter look for types? How does python interpreter know the type of a variable? I’m not looking how do get the type. I’m here looking at what happens behind the scene. …

Total answers: 3

Why does '() is ()' return True when '[] is []' and '{} is {}' return False?

Why does '() is ()' return True when '[] is []' and '{} is {}' return False? Question: From what I’ve been aware of, using [], {} or () to instantiate objects returns a new instance of list, dict or tuple respectively; a new instance object with a new identity. This was pretty clear to …

Total answers: 1

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

Why is str.strip() so much faster than str.strip(' ')? Question: Splitting on white-space can be done in two ways with str.strip. You can either issue a call with no arguments, str.strip(), which defaults to using a white-space delimiter or explicitly supply the argument yourself with str.strip(‘ ‘). But, why is it that when timed these …

Total answers: 2

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

Two variables in Python have same id, but not lists or tuples Question: Two variables in Python have the same id: a = 10 b = 10 a is b >>> True If I take two lists: a = [1, 2, 3] b = [1, 2, 3] a is b >>> False according to this …

Total answers: 5

When is hash(n) == n in Python?

When is hash(n) == n in Python? Question: I’ve been playing with Python’s hash function. For small integers, it appears hash(n) == n always. However this does not extend to large numbers: >>> hash(2**100) == 2**100 False I’m not surprised, I understand hash takes a finite range of values. What is that range? I tried …

Total answers: 4

What exactly is __weakref__ in Python?

What exactly is __weakref__ in Python? Question: Surprisingly, there’s no explicit documentation for __weakref__. Weak references are explained here. __weakref__ is also shortly mentioned in the documentation of __slots__. But I could not find anything about __weakref__ itself. What exactly is __weakref__? – Is it just a member acting as a flag: If present, the …

Total answers: 3

Why are Python's arrays slow?

Why are Python's arrays slow? Question: I expected array.array to be faster than lists, as arrays seem to be unboxed. However, I get the following result: In [1]: import array In [2]: L = list(range(100000000)) In [3]: A = array.array(‘l’, range(100000000)) In [4]: %timeit sum(L) 1 loop, best of 3: 667 ms per loop In …

Total answers: 5

Why is code using intermediate variables faster than code without?

Why is code using intermediate variables faster than code without? Question: I have encountered this weird behavior and failed to explain it. These are the benchmarks: py -3 -m timeit "tuple(range(2000)) == tuple(range(2000))" 10000 loops, best of 3: 97.7 usec per loop py -3 -m timeit "a = tuple(range(2000)); b = tuple(range(2000)); a==b" 10000 loops, …

Total answers: 2

Runtime of python's if substring in string

Runtime of python's if substring in string Question: What is the big O of the following if statement? if “pl” in “apple”: … What is the overall big O of how python determines if the string “pl” is found in the string “apple” or any other substring in string search. Is this the most efficient …

Total answers: 4