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 ...
17
votes
1
answer
4k
views
How do chained comparisons in Python actually work?
The Python Doc for Comparisons says:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z ...
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 ...
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
134
views
Question about the 'non-linear' behaviour of the Python interpreter in Jupyter
I'm running the following code both remotely on a linux machine via ssh, and on the same linux machine as a Jupyter notebook accessed through a browser.
import cv2
import pdf2image
def minimalFun(...
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 ...
388
votes
6
answers
117k
views
How does asyncio actually work?
This question is motivated by my another question: How to await in cdef?
There are tons of articles and blog posts on the web about asyncio, but they are all very superficial. I couldn't find any ...
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__ ...
2
votes
1
answer
308
views
How does CPython determine whether a user supplied an optional argument?
I started wondering how CPython can tell the difference between None as a default argument and None as a specified argument.
For example, dict.pop() will throw a KeyError if the key doesn't exist. ....
37
votes
3
answers
8k
views
Python frozenset hashing algorithm / implementation
I'm currently trying to understand the mechanism behind the hash function defined for Python's built-in frozenset data type. The implementation is shown at the bottom for reference. What I'm ...
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 ...
1305
votes
14
answers
442k
views
Usage of __slots__?
What is the purpose of __slots__ in Python — especially with respect to when I would want to use it, and when not?
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, ...