Enhanced to be usable for python objects in addition to C++ objects.