$$ \newcommand{\problemdivider}{\begin{center}\large \bf\ldots\ldots\ldots\ldots\ldots\ldots\end{center}} \newcommand{\subproblemdivider}{\begin{center}\large \bf\ldots\ldots\end{center}} \newcommand{\pdiv}{\problemdivider} \newcommand{\spdiv}{\subproblemdivider} \newcommand{\ba}{\begin{align*}} \newcommand{\ea}{\end{align*}} \newcommand{\rt}{\right} \newcommand{\lt}{\left} \newcommand{\bp}{\begin{problem}} \newcommand{\ep}{\end{problem}} \newcommand{\bsp}{\begin{subproblem}} \newcommand{\esp}{\end{subproblem}} \newcommand{\bssp}{\begin{subsubproblem}} \newcommand{\essp}{\end{subsubproblem}} \newcommand{\atag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\arabic{section}.\alph{subsection}.\alph{equation}}} \newcommand{\btag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\arabic{section}.\alph{equation}}} \newcommand{\ctag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\arabic{equation}}} \newcommand{\dtag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\Alph{chapter}.\arabic{section}.\arabic{equation}}} \newcommand{\unts}[1]{\ \text{#1}} \newcommand{\textop}[1]{\operatorname{#1}} \newcommand{\textopl}[1]{\operatornamewithlimits{#1}} \newcommand{\prt}{\partial} \newcommand{\pderi}[3]{\frac{\prt^{#3}#1}{\prt #2^{#3}}} \newcommand{\deri}[3]{\frac{d^{#3}#1}{d #2^{#3}}} \newcommand{\del}{\vec\nabla} \newcommand{\exval}[1]{\langle #1\rangle} \newcommand{\bra}[1]{\langle #1|} \newcommand{\ket}[1]{|#1\rangle} \newcommand{\ham}{\mathcal{H}} \newcommand{\arr}{\mathfrak{r}} \newcommand{\conv}{\mathop{\scalebox{2}{\raisebox{-0.2ex}{$\ast$}}}} \newcommand{\bsm}{\lt(\begin{smallmatrix}} \newcommand{\esm}{\end{smallmatrix}\rt)} \newcommand{\bpm}{\begin{pmatrix}} \newcommand{\epm}{\end{pmatrix}} \newcommand{\bdet}{\lt|\begin{smallmatrix}} \newcommand{\edet}{\end{smallmatrix}\rt|} \newcommand{\bs}[1]{\boldsymbol{#1}} \newcommand{\uvec}[1]{\bs{\hat{#1}}} \newcommand{\qed}{\hfill$\Box$} $$
Tags:

C/C++ integration

Boost is the best way to do this.

On passing objects from Boost to Python: https://stackoverflow.com/questions/31468724/python-to-boostpythonobject

To pass objects from Python to Boost, the .ptr() method of boost::python::object will return a pointer to the underlying PyObject, which can be manipulated via standard downstream methods.

To debug C++ functions in Python, see: https://stackoverflow.com/questions/38898459/debugging-python-and-c-exposed-by-boost-together

Dumb tricks

Make a blank object

Sometimes we need a blank object, e.g. when interactively running code that refers to self:

class Foo:
	def bar():
		self.x = 2
		self.y = baz(self.x)

In a REPL, this will of course error:

In [1]: self.x = 2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-c1ab64b90a9f> in <module>
----> 1 self.x = 2

NameError: name 'self' is not defined

We can get around this like so:

In [2]: self = lambda: None

In [3]: self.x = 2

Monkey patching instance method

To replace the bar method in instance f:

class Foo:
     def bar(self):
         pass

f = Foo()
def baz(self):
     print("baz")

f.bar = baz.__get__(f, Foo)

In other words:

<instance method> = <new function>.__get__(<instance>, <class>)

List comprehensions in debugger

Need to explicitly add any external variables used in the list comprehension to globals(), i.e.

y = [1, 2, 3]
y1 = [x + 1 for x in y] # does not work in a debugger

globals()["y"] = y
y1 = [x + 1 for x in y] # now works

Bug report here.

Regex

To have backreferences next to a number, use the \g<...> syntax, e.g.

X.str.replace("foo(.*)1234", r"foo\g<1>1234", regex = True)

Weirdness

In [38]: 4*[]
Out[38]: []

In [39]: [4*[]]
Out[39]: [[]]

In [40]: 4*[[]]
Out[40]: [[], [], [], []]