If we look at the resize behavior for sets under 50k elements: This pattern is consistent with quadrupling of the backing storage size once the set is 3/5ths full, plus some presumably constant overhead for the PySetObject: A similar pattern continues even for larger sets, but the resize factor switches to doubling instead of quadrupling. The reported size for small
Tag: python-internals
Python generator expression recursion
This is a question about Python internals. The following code is taken from this video about laziness in python: The sieve function generates prime numbers lazily (read this for the original concept). Conceptually, we add filters to the “sieve”, so every number (say, 10) is tested against all the previously found prime numbers (so 2, 3, 5 and 7) until
Why is arithmetic not supported for dict? Usupported operand type(s) for +: ‘dict’ and ‘dict’
In Python, there is a possibility to sum lists and tuples, e.g. But trying to do the same with dicts will raise: I guess there could be the same behavior as update() has for cases when merging two dicts with the same key: Why these operands aren’t implemented? Any optimization issues or just by design? Answer In Python 3.9 was
How does Python’s reversed() function work?
According to Python’s docs, reversed() uses __getitem__ and __len__ if __reversed__ is not implemented. I’ve encountered a weird behavior and failed to explain it: Although calling reversed() on mapping types makes no sense, how does it know it’s a mapping? Does it internally check isinstance(inst, dict)? Does it check for any general mapping like collections.abc.Mapping? Is there any way to
delete the hashed node in deque in O(1)
How do I hash a built-in node in deque (which is a double linked list) and delete the node in the middle in O(1)? Is the built-in node exposed? For example, I want to save a deque’s node in dict so I can delete the node in constant time later. This is a use case in LRU, using deque so
Why doesn’t Python have a “__req__” (reflected equality) method?
I have a little helper class: This lets me do sweet magic like: without having to use a list comprehension (as in np.array(x in (2,3) for x in arr). (I maintain a UI that lets (trusted) users type in arbitrary code, and a == AnyOf(1,2,3) is a lot more palatable than a list comprehension to the non-technically savvy user.) However!
How does a generator function work internally?
Below is a generator function. Does this generator function (f) get implemented, internally, as shown below? Edit: This is the answer for my question. Answer Internally, a generator works about the same as a regular function call. Under-the-hood, running generators and running functions use mostly the same machinery. When you call either a function or a generator, a stackframe is
subclassing dict; dict.update returns incorrrect value – python bug?
I needed to make a class that extended dict and ran into an interesting problem illustrated by the dumb example in the image below. Why is d.update() ignoring the class’s __getitem__? EDIT: This is in python2.7 which does not appear to contain collections.UserDict Thinking UserDict.UserDict is the equivalent I tried this, and it gets closer, but still behaves interestingly. Answer
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 result: What could be the cause of such a difference? Answer The storage is “unboxed”, but every time you access an element Python has to “box” it (embed it in a regular Python object) in order to do anything with it.
Why round off of 0.500000 in python differs from 45.500000 using ‘%.0f’?
Recently, I learned art of string formatting in Python 2.7. I decided to play with floating point numbers. Came across an awkward looking solution, as written below. BUT Please help me understand, why this behavior is shown by %f. Answer The internal implementation for the %.0f string format uses a round-half-even rounding mode. In Python 2, the round() function uses