Top "Python-internals" questions

How does Python work underneath the hood?

Why is 'x' in ('x',) faster than 'x' == 'x'?

>>> timeit.timeit("'x' in ('x',)") 0.04869917374131205 >>> timeit.timeit("'x' == 'x'") 0.06144205736110564 Also works for tuples with …

python performance python-3.x python-internals
Python string interning

While this question doesn't have any real use in practice, I am curious as to how Python does string interning. …

python string python-internals internals string-interning
Why are Python's arrays slow?

I expected array.array to be faster than lists, as arrays seem to be unboxed. However, I get the following …

python arrays performance boxing python-internals
What exactly is __weakref__ in Python?

Surprisingly, there's no explicit documentation for __weakref__. Weak references are explained here. __weakref__ is also shortly mentioned in the documentation …

python python-3.x python-internals
Meaning of unittest.main() in Python unittest module

I was trying to learn unit testing in Python, specifically the unittest module. Consider the following lines: import unittest class …

python unit-testing python-internals
Why is the order in dictionaries and sets arbitrary?

I don't understand how looping over a dictionary or set in python is done by 'arbitrary' order. I mean, it's …

python dictionary set python-internals
Why is a class __dict__ a mappingproxy?

I wonder why a class __dict__ is a mappingproxy, but an instance __dict__ is just a plain dict >>&…

python python-3.x class dictionary python-internals
Why is x**4.0 faster than x**4 in Python 3?

Why is x**4.0 faster than x**4? I am using CPython 3.5.2. $ python -m timeit "for x in range(100):" " x**4.0" 10000 loops, best …

python performance python-3.x python-3.5 python-internals
How does swapping of members in tuples (a,b)=(b,a) work internally?

In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of …

python tuples python-internals iterable-unpacking
'order' of unordered Python sets

I understand that sets in Python are unordered, but I'm curious about the 'order' they're displayed in, as it seems …

python python-internals