python-internals

Why doesn't Python have a "__req__" (reflected equality) method?

Why doesn't Python have a "__req__" (reflected equality) method? Question: I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: >>> arr = np.array([1,2,3,4,5]) >>> arr == AnyOf(2,3) np.array([False, True, True, False, False]) without having to use …

Total answers: 3

Why does the size of this Python String change on a failed int conversion

Why does the size of this Python String change on a failed int conversion Question: From the tweet here: import sys x = ‘ñ’ print(sys.getsizeof(x)) int(x) #throws an error print(sys.getsizeof(x)) We get 74, then 77 bytes for the two getsizeof calls. It looks like we are adding 3 bytes to the object, from the failed …

Total answers: 1

Why do tuples take less space in memory than lists?

Why do tuples take less space in memory than lists? Question: A tuple takes less memory space in Python: >>> a = (1,2,3) >>> a.__sizeof__() 48 whereas lists takes more memory space: >>> b = [1,2,3] >>> b.__sizeof__() 64 What happens internally on the Python memory management? Asked By: JON || Source Answers: I assume …

Total answers: 4

Why are reversed and sorted of different types in Python?

Why are reversed and sorted of different types in Python? Question: reversed‘s type is “type”: >>> type(reversed) <class ‘type’> sorted‘s type is “builtin function or method”: >>> type(sorted) <class ‘builtin_function_or_method’> However, they seem the same in nature. Excluding the obvious difference in functionality (reversing vs. sorting sequences), what’s the reason for this difference in implementation? …

Total answers: 2

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

Why is repr(int) faster than str(int)? Question: I am wondering why repr(int) is faster than str(int). With the following code snippet: ROUNDS = 10000 def concat_strings_str(): return ”.join(map(str, range(ROUNDS))) def concat_strings_repr(): return ”.join(map(repr, range(ROUNDS))) %timeit concat_strings_str() %timeit concat_strings_repr() I get these timings (python 3.5.2, but very similar results with 2.7.12): 1.9 ms ± 17.9 µs …

Total answers: 3

Why is deque implemented as a linked list instead of a circular array?

Why is deque implemented as a linked list instead of a circular array? Question: CPython deque is implemented as a doubly-linked list of 64-item sized “blocks” (arrays). The blocks are all full, except for the ones at either end of the linked list. IIUC, the blocks are freed when a pop / popleft removes the …

Total answers: 2

Does declaring variables in a function called from __init__ still use a key-sharing dictionary?

Does declaring variables in a function called from __init__ still use a key-sharing dictionary? Question: I’ve tried to always declare class attributes inside the __init__ for clarity and organizational reasons. Recently, I’ve learned that strictly following this practice has extra non-aesthetic benefits too thanks to PEP 412 being added for Python 3.3. Specifically, if all …

Total answers: 2

Why are dict lookups always better than list lookups?

Why are dict lookups always better than list lookups? Question: I was using a dictionary as a lookup table but I started to wonder if a list would be better for my application — the amount of entries in my lookup table wasn’t that big. I know lists use C arrays under the hood which …

Total answers: 3

Does Python optimize away a variable that's only used as a return value?

Does Python optimize away a variable that's only used as a return value? Question: Is there any ultimate difference between the following two code snippets? The first assigns a value to a variable in a function and then returns that variable. The second function just returns the value directly. Does Python turn them into equivalent bytecode? …

Total answers: 2

Python's Passing by References

Python's Passing by References Question: Hello I am trying to understand how Python’s pass by reference works. I have an example: >>>a = 1 >>>b = 1 >>>id(a);id(b) 140522779858088 140522779858088 This makes perfect sense since a and b are both referencing the same value that they would have the identity. What I dont quite understand …

Total answers: 3