A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20120109/f8f2c5c6/attachment.html below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transational//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
</HEAD>
<BODY>Hello,<br><br>I am trying to send a tuple to a method of a python class and I got a Run failed from netbeans compiler<br>when I want to send a tuple to a simple method in a module it works,when I want to send a simple parameter to a method of a clas it works also but not a tuple to a method of a class<br>I put the code below,thanks for all the possible suggestions<br><br>Python Code:<br><br>class cVector:<br>&nbsp; def __init__(self,msg):<br>&nbsp;&nbsp;&nbsp; self.value = msg<br>&nbsp; def ComputeNorm(self,vecData):<br>&nbsp;&nbsp;&nbsp; #don't use vecData for instance<br>&nbsp;&nbsp;&nbsp; result = 12.<br>&nbsp;&nbsp;&nbsp; return(result)<br><br><br>C++ Code :<br>&nbsp; PyObject *ret, *mymod, *pclass, *method, *args, *object;<br>&nbsp; float retValue;<br><br>&nbsp; Py_Initialize();<br>&nbsp; //PySys_SetPath("/home/pascal/projPytCpp/proj1");<br>&nbsp; PySys_SetPath(".");<br>&nbsp; <br>&nbsp; // Module<br>&nbsp; mymod = PyImport_ImportModule("mModule8");<br>&nbsp; if (mymod == NULL){<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Can't Open a module:\n" ;<br>&nbsp;&nbsp;&nbsp; Py_DECREF(mymod);<br>&nbsp; }<br>&nbsp; <br>&nbsp; // Class<br>&nbsp; pclass = PyObject_GetAttrString(mymod, "cVector");<br>&nbsp; if (pclass == NULL) {<br>&nbsp;&nbsp;&nbsp; Py_DECREF(pclass);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Can't find class\n";<br>&nbsp; }<br>&nbsp; <br>&nbsp; // Parameters/Values<br>&nbsp; args = Py_BuildValue("(f)", 100.0);<br>&nbsp; if (args == NULL) {<br>&nbsp;&nbsp;&nbsp; Py_DECREF(args);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Can't build argument list for class instance\n";<br>&nbsp; }<br>&nbsp; <br>&nbsp; // Object with parameter/value<br>&nbsp; object = PyEval_CallObject(pclass, args);<br>&nbsp; if (object == NULL) {<br>&nbsp;&nbsp;&nbsp; Py_DECREF(object);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Can't create object instance:\n";<br>&nbsp; }<br>&nbsp; <br>&nbsp; // Decrement the argument counter as we'll be using this again <br>&nbsp; Py_DECREF(args);<br>&nbsp; <br>&nbsp; // Get the object method - note we use the object as the object<br>&nbsp; // from which we access the attribute by name, not the class <br>&nbsp; method = PyObject_GetAttrString(object, "ComputeNorm");<br>&nbsp; if (method == NULL) {<br>&nbsp;&nbsp;&nbsp; Py_DECREF(method);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Can't find method\n";<br>&nbsp; }<br>&nbsp; <br>&nbsp; // Decrement the counter for our object, since we now just need<br>&nbsp; // the method reference <br>&nbsp; Py_DECREF(object);<br><br>&nbsp; // Build our argument list - an empty tuple because there aren't<br>&nbsp; // any arguments <br>&nbsp; <br>&nbsp; cout &lt;&lt; "Prepare the Tuple:\n" ;<br>&nbsp; // WE pass a tuple<br>&nbsp; args = PyTuple_New( 3 );<br>&nbsp; if (args == NULL) {<br>&nbsp;&nbsp;&nbsp; Py_DECREF(args);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Can't build argument list for method call\n";<br>&nbsp; }<br>&nbsp; <br>&nbsp; PyObject&nbsp; *py_argument;<br>&nbsp; // 1st argument <br>&nbsp; py_argument = PyFloat_FromDouble(5.);<br>&nbsp; PyTuple_SetItem(args, 0, py_argument);<br>&nbsp;<br>&nbsp; // 2nd argument <br>&nbsp; py_argument = PyFloat_FromDouble(10.);<br>&nbsp; PyTuple_SetItem(args, 1, py_argument);<br>&nbsp; <br>&nbsp; // 3nd argument <br>&nbsp; py_argument = PyFloat_FromDouble(15.);<br>&nbsp; PyTuple_SetItem(args, 2, py_argument);<br>&nbsp; <br>&nbsp; cout &lt;&lt; "Before the Exec:\n" ;<br>&nbsp; // Call our object method with arguments (try two ways to do it any works)<br>&nbsp; //ret = PyEval_CallObject(method,args);<br>&nbsp; ret = PyObject_CallObject(method,args);<br>&nbsp; if (ret == NULL) {<br>&nbsp;&nbsp;&nbsp; Py_DECREF(ret);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Couldn't call method\n";<br>&nbsp; }<br>&nbsp; <br><br>&nbsp; // Convert the return value back into a C variable and display it <br>&nbsp; PyArg_Parse(ret, "f", &amp;retValue);<br>&nbsp; printf("Farenheit: %f\n", retValue);<br>&nbsp; // Kill the remaining objects we don't need <br>&nbsp; Py_DECREF(method);<br>&nbsp; Py_DECREF(ret);<br>&nbsp; // Close off the interpreter and terminate <br>&nbsp; Py_Finalize();<br>&nbsp; <br></BODY></HTML>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4