cpython

Why cpython exposes 'PyTuple_SetItem' as C-API if tuple is immutable by design?

Why cpython exposes 'PyTuple_SetItem' as C-API if tuple is immutable by design? Question: Tuple in python is immutable by design, so if we try to mutate a tuple object python emits following TypeError which make sense. >>> a = (1, 2, 3) >>> a[0] = 12 Traceback (most recent call last): File "<stdin>", line 1, …

Total answers: 2

Why does this specific code run faster in Python 3.11?

Why does this specific code run faster in Python 3.11? Question: I have the following code in a Python file called benchmark.py. source = """ for i in range(1000): a = len(str(i)) """ import timeit print(timeit.timeit(stmt=source, number=100000)) When I tried to run with multiple python versions I am seeing a drastic performance difference. C:UsersUsernameDesktop>py -3.10 …

Total answers: 1

CPython list.insert(index) real time complexity, when all inserts occur at same index?

CPython list.insert(index) real time complexity, when all inserts occur at same index? Question: My noob question is in the title I have some code that iteratively builds a list. All inserts are always at the second-to-last position. Is list.insert(-1, value) truly an O(1) operation in the CPython implementation? O(?) -> what I want to do …

Total answers: 1

In Cpython implementation, are strings an array of characters or array of references/pointers like python lists?

In Cpython implementation, are strings an array of characters or array of references/pointers like python lists? Question: I went through the post https://rushter.com/blog/python-strings-and-memory/ Based on that article, Depending on the type of characters in a string, each character in that string would be represented using either 1/2/4 bytes Since the address length of each such …

Total answers: 1

using datetime module to create pause-able timer getting weird results

using datetime module to create pause-able timer getting weird results Question: I’m trying to create a pause-able timer in Python. During my search I came across this answer. https://stackoverflow.com/a/60027719/11892518 I modified the code a little and added the timer in a while loop I also used the keyboard module for human interaction while the loop …

Total answers: 1

What is the purpose of ','. expression in Python grammar specification?

What is the purpose of ','. expression in Python grammar specification? Question: Can you help me with the args part in Python grammar specification? args: | ‘,’.(starred_expression | ( assignment_expression | expression !’:=’) !’=’)+ [‘,’ kwargs ] | kwargs Especially this part: ‘,’.(starred_expression | ( assignment_expression | expression !’:=’) !’=’)+. What does ‘,’. mean? I …

Total answers: 1

Swig pass Python datetime object to C++

Swig pass Python datetime object to C++ Question: I have a function in C++ that is passed a uint64_t as nanoseconds from epoch. I have wrapped this number in an object DateTime as in struct DateTime { uint64_t epochns; }; void print( DateTime ts ); Obviously the function is not exactly print as I could …

Total answers: 1

The `[]` operator more than syntactic sugar?

The `[]` operator more than syntactic sugar? Question: The most common syntax of slice objects seems to be int:int:int, and specifically within the [] operator of tuples lists and strs. It makes sense why this syntax can’t be used to instantiate a slice, Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux Type …

Total answers: 1

When I write an int64 type number to the function, a different number is returned

When I write an int64 type number to the function, a different number is returned Question: I converted the golang code to c code and called it from python. but when the function should return a number close to the number I wrote inside, it returns a very different number. main.py import ctypes library = …

Total answers: 1