Checking the type of a PyObject
Rainer Deyke
root at rainerdeyke.com
Sun Aug 12 01:11:03 EDT 2001
"wim delvaux" <wim.delvaux at adaptiveplanet.com> wrote in message
news:3B75D4D4.61B32D80 at adaptiveplanet.com...
> Hi all,
>
> just looking at python and wondering if I could integrate it.
>
> While thinking I was wondering if one could determine the type of a
> PyObject
>
> Example : calling from python
>
> Mod.Function( 1234 )
> Mod.Function( "string" );
>
> So Function should be callable with 2 different types (kind of
> overloading)
>
> in C (Making abstraction of a lot of other stuff )
>
> Mod_Function( PyObject *self, PyObject *args ) {
>
> PyObject * FirstArg;
>
> if (!PyArg_ParseTuple(args, "o", &FirstArg ))
> return NULL;
>
> /* And then something like */
>
> if( Py_GetType( FirstArg ) == String ) {
> char * String;
> PyArg_ParseTuple( args, "s", &String );
> } else if ( Py_GetType( FirstArg ) == Integer ) {
> long IVal;
> PyArg_ParseTuple( args, "i", &IVal);
> } else {
> return NULL;
> }
> }
>
> What would be the correct Python Library code to implement the above
> pseudo code
One approach would be the obvious:
Mod_Function( PyObject *self, PyObject *args ) {
char * String;
long IVal;
if (PyArg_ParseTuple( args, "s", &String )) {
/* do something with String */
} else if (PyArg_ParseTuple( args, "i", &IVal)) {
/* do something with IVal */
} else {
return NULL;
}
}
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list