When I use bitwise and operator(&) with the number of 1 to find out if a number x is odd or even (x & 1), does the interpreter change the binary representation of 1 according to the binary representation of x? For example: 2 & 1 -> 10 & 01 -> then perform comparison bitwise 5 & 1 -> 101
Tag: cpython
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
Rocksdb undefined symbol: ZSTD_versionNumber Cpython
I am using python 3.6, and RocksDB is installed in the container by following command: It looks like a version issue for me. Not sure how to resolve it. Tried add rocksdb and python-rocksdb to the requirement file. But not work either. The service was working before. However, the last working docker container was built a year ago. Many error
Create an instance of a class N times without assigning it to a variable
CPython 3.8 Seems like it’s an interpreter magic that definitely has something to do with garbage collection. Is it CPython specific, not guaranteed by any standard behavior? Answer Given two distinct objects x and y (i.e., x is y is false), id(x) != id(y) is only guaranteed if the life times of x and y overlap. If they do not
A full and minimal example for a class (not method) with Python C Extension?
Everywhere, I can easily find an example about writing a method with Python C Extensions and use it in Python. Like this one: Python 3 extension example How to do write a hello word full featured Python class (not just a module method)? I think this How to wrap a C++ object using pure Python Extension API (python3)? question has
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
Do attribute names consume memory on instance basis in python
Considering I have millions of objects with 3 __slots__ Is it more memory efficient to have short slot names like x vs. long like would_you_like_fries_with_that_cheeseburger? Or are the names allocated only once per class (opposed to once per instance?) Answer Names for slots only take memory per class, not per instance. Slots use descriptors that map directly into the memory
Python string ‘in’ operator implementation algorithm and time complexity
I am thinking of how the in operator implement, for instance In CPython, which algorithm is used to implement the string match, and what is the time complexity? Is there any official document or wiki about this? Answer It’s a combination of Boyer-Moore and Horspool. You can view the C code here: Fast search/count implementation, based on a mix between
Why do up and down arrow commands not work in the Python command line interpreter?
I am using a VT100 terminal emulator on Linux. In bash, up and down arrows scroll through the last commands executed; they work as expected. Previous (up arrow) and next (down arrow) commands are not interpreted in the Python command line interpreter. What kind of key mappings do I need to make this work? Thank you. Answer I think I’ve