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
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
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>)
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.
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)
In [38]: 4*[]
Out[38]: []
In [39]: [4*[]]
Out[39]: [[]]
In [40]: 4*[[]]
Out[40]: [[], [], [], []]