767 questions
3
votes
2
answers
186
views
Why does summing a complex and a np.float64 value give a complex value instead of a np.complex128 value suggested by the Numerical promotion rules?
I am reading about Numerical promotion in numpy. I copy the main graph here:
Based on the graph, this explanation:
The input dtype with the higher kind determines the kind of the result dtype. The ...
3
votes
0
answers
99
views
How do I get a list of all instance attributes of a Python object? [duplicate]
Given some object o in Python, how do I get a list of all instance (not class) attributes which that object has?
For most objects, this is simple: just check o.__dict__, either directly or via vars(). ...
4
votes
2
answers
156
views
Compile list with more than one argument
Why is the following line valid Python code?
code = compile('l = list(1, 2, 3, 4, 5)', '', 'exec')
The documentation says
Using the type constructor: list() or list(iterable)
I can even write the ...
Advice
1
vote
8
replies
141
views
What is the rationale for having two distinct operators (*args and **kwargs) for arbitrary arguments in Python?
I am curious about the design philosophy of Python's function arguments.
Currently, we have *args to collect positional arguments into a tuple and **kwargs to collect keyword arguments into a ...
Advice
3
votes
6
replies
140
views
Why does Python attempt item assignment after in-place += on a mutable element inside a tuple?
Context
In Fluent Python (2nd ed.) by Luciano Ramalho, Example 2-16 shows that:
>>> t = (1, 2, [30, 40])
>>> t[2] += [50, 60]
results in:
t becoming (1, 2, [30, 40, 50, 60])
a ...
5
votes
3
answers
241
views
python function argument set reuse, with variations in internal code
I have two functions with 100% identical (and lengthy) sets of arguments, but slight variations in internal code.
I'd like to use the first-class-object nature of functions to reuse the argument set, ...
0
votes
2
answers
132
views
Python exec and Git's worktree lifecycle
I'm encountering a peculiar issue when using Python's exec function within scripts that are run across different Git worktrees. Specifically, I've observed that the behavior of exec (and by extension, ...
2
votes
0
answers
117
views
How do I link a custom SQLite and not the system version when building Python? [closed]
I am trying to link a custom version of sqlite3 when building Python, but there is an error when compiling Python:
/usr/bin/install: cannot stat 'Modules/_sqlite3.cpython-313-x86_64-linux-gnu.so': No ...
2
votes
0
answers
210
views
What is the documented behaviour (if any) when someone imports `pkg.__init__[ as pkg]` instead of `import pkg`?
To be clear, I'm not suggesting anyone actually should import pkg.__init__ directly. This is to understand potential pitfalls if someone decides to convert a module-only distribution into a package, ...
0
votes
0
answers
55
views
Why does Python 2 forbid implicit __hash__ on old-style classes only?
When I override equality on a class in any language I have to be careful not to create an inconsistent hash function.
Python 3 and Python 2 with old-style classes help here by deleting the __hash__ ...
3
votes
1
answer
225
views
What is the length of a python bytecode instruction in CPython?
Python docs on the dis module state that the length of a python bytecode instruction in CPython is 2 bytes (https://docs.python.org/3/library/dis.html)
However, when I disassemble a function and look ...
5
votes
2
answers
315
views
Is L[a:b]=L[c:d] optimized in Python to avoid creating a new list?
I am unable to find anything on the official Python documentation whether
L[a:b] = L[c:d]
creates a new (temporary) list for L[c:d] before the in-place modification of L. The tutorial says that:
All ...
1
vote
0
answers
80
views
How is import os.path possible? [duplicate]
Since os is a module instead of a package, import os.path should fail. For comparison:
>>> import os.sys
Traceback (most recent call last):
File "<python-input-0>", line 1, ...
2
votes
2
answers
182
views
How is `self` accessed in Python methods?
I am wondering how self works under-the-hood in Python classes.
My current limited understanding is that when a class is defined, e.g.
class Foo:
def __init__(self, x: int):
self.x = x
...
1
vote
1
answer
203
views
How can I store ids in Python without paying the 28-byte-per-int price?
My Python code stores millions of ids in various data structures, in order to implement a classic algorithm. The run time is good, but the memory usage is awful.
These ids are ints. I assume that ...