Skip to content
Advertisement

Tag: python-internals

How are small sets stored in memory?

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

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

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

Advertisement