Exposing a function with a default argument to python

LyTning94

Dragonborn
Joined
Nov 10, 2010
Messages
397
Location
Skyrim
Can I expose a function with default argument to python? I have a function called switchMap() which takes an int argument that defaults to -1 if none is provided. I exposed this function to python like this:

Code:
.def("switchMap", &CyGlobalContext::switchMap, "void (int iIndex)")

When this is called from python, will you stil be able to call it without supplying a value for iIndex?
 
Can I expose a function with default argument to python? I have a function called switchMap() which takes an int argument that defaults to -1 if none is provided. I exposed this function to python like this:

Code:
.def("switchMap", &CyGlobalContext::switchMap, "void (int iIndex)")

When this is called from python, will you stil be able to call it without supplying a value for iIndex?
Not by default, but you can get the behavior with a bit of extra syntax as described here:
http://www.boost.org/doc/libs/1_32_...ython/functions.html#python.default_arguments
 
Thanks for the link. From what I understand, then, the whole thing (I have multiple functions to which this applies) should end up looking like this?

Code:
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(gc_overloads, getMap, 0, 1)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(gc_overloads, switchMap, 0, 1)

void CyGlobalContextPythonInterface1(python::class_<CyGlobalContext>& x)
{
	OutputDebugString("Python Extension Module - CyGlobalContextPythonInterface1\n");

	x
		.def("getNumMapInfos", &CyGlobalContext::getNumMapInfos, "int () - Number of maps")
		.def("getMapInfo", &CyGlobalContext::getMapInfo, python::return_value_policy<python::reference_existing_object<(), "(MapID) - CvInfo for Map")
		.def("switchMap", &CyGlobalContext::switchMap, gc_overloads())

// MODIFIED
		.def("getMap", &CyGlobalContext::getCyMap, python::return_value_policy<python::reference_existing_object>(), gc_overloads())

Do I have to use different classes for each function, instead of gc_overloads for both?
 
Thanks for the link. From what I understand, then, the whole thing (I have multiple functions to which this applies) should end up looking like this?

Code:
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(gc_overloads, getMap, 0, 1)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(gc_overloads, switchMap, 0, 1)

void CyGlobalContextPythonInterface1(python::class_<CyGlobalContext>& x)
{
	OutputDebugString("Python Extension Module - CyGlobalContextPythonInterface1\n");

	x
		.def("getNumMapInfos", &CyGlobalContext::getNumMapInfos, "int () - Number of maps")
		.def("getMapInfo", &CyGlobalContext::getMapInfo, python::return_value_policy<python::reference_existing_object<(), "(MapID) - CvInfo for Map")
		.def("switchMap", &CyGlobalContext::switchMap, gc_overloads())

// MODIFIED
		.def("getMap", &CyGlobalContext::getCyMap, python::return_value_policy<python::reference_existing_object>(), gc_overloads())

Do I have to use different classes for each function, instead of gc_overloads for both?
I think you have to use different names for them.
 
Back
Top Bottom