If you tell Python it’ll run x.__add__(5), which handles the int 5. But what about int.__add__ doesn’t know how to handle x. So it returns NotImplemented — and Python turns it around, calling: That’s the “reverse add” magic method!
If your Python class implements __add__, it should usually return a new instance of your class, not an int:
How can your Python object support +? Implement __add__:
Operators in Python are turned into “magic” method calls: x + y # becomes x.__add__(y) So: Different errors from different methods!
Because Python functions’ defaults are kept in __defaults__, avoid mutable defaults: add1.__defaults__ # ([1, 1, 1],)
When a Python generator yields, its stack frame remains — inspect it for the current line and local variables:
July and August are often when people take a break or go on vacation. But for me, this summer has been super busy, full of writing and improving LernerPython based on feedback I’ve gotten from people around the world. And so, I’m here with some big changes I’m making at LernerPython World Headquarters: 1. AI…
Using PyArrow dtypes in Python Pandas isn’t always faster: %timeit s_pyarr.mean() # 21.5ms%timeit s_np.mean() # 47.3ms %timeit s_pyarr.nlargest(10) # 667ms%timeit s_np.nlargest(10) # 663ms
PyArrow strings in Python Pandas are smaller than Python strings. But they’re also far faster: %timeit s_pyarr.str.len() # 106 µs%timeit s_py.str.len() # 1.6 ms In many examples, PyArrow was far faster.
Want PyArrow dtypes in your Python Pandas data frame? The dtypes are double[pyarrow], int64[pyarrow], and string[pyarrow], not the normal NumPy ones. Note: This is still experimental… but it’s also the future.