|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do I call an object's method from C?
Answer: The PyObject_CallMethod() function can be used to call an arbitrary method of an object. The parameters are the object, the name of the method to call, a format string like that used with Py_BuildValue(), and the argument values:
PyObject * PyObject_CallMethod(PyObject *object, char *method_name, char *arg_format, ...);
This works for any object that has methods -- whether built-in or user-defined. You are responsible for eventually Py_DECREF'ing the return value.
To call, e.g., a file object's "seek" method with arguments 10, 0 (assuming the file object pointer is "f"):
res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0); if (res == NULL) { ... an exception occurred ... } else { Py_DECREF(res); }
Note that since PyObject_CallObject() always wants a tuple for the argument list, to call a function without arguments, pass "()" for the format, and to call a function with one argument, surround the argument in parentheses, e.g. "(i)".
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8853 users |
Added on |
9/24/2014 |
Views |
71706 |
Rate it! |
|
|
Question:
How do I call an object's method from C?
Answer:
The PyObject_CallMethod() function can be used to call an arbitrary method of an object. The parameters are the object, the name of the method to call, a format string like that used with Py_BuildValue(), and the argument values:
PyObject * PyObject_CallMethod(PyObject *object, char *method_name, char *arg_format, ...);
This works for any object that has methods -- whether built-in or user-defined. You are responsible for eventually Py_DECREF'ing the return value.
To call, e.g., a file object's "seek" method with arguments 10, 0 (assuming the file object pointer is "f"):
res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0); if (res == NULL) { ... an exception occurred ... } else { Py_DECREF(res); }
Note that since PyObject_CallObject() always wants a tuple for the argument list, to call a function without arguments, pass "()" for the format, and to call a function with one argument, surround the argument in parentheses, e.g. "(i)". Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How do I extract C values from a Python object?
|
View Answer
|
|
How can I evaluate an arbitrary Python expression from C?
|
View Answer
|
|
How can I execute arbitrary Python statements from C?
|
View Answer
|
|
Can I create my own functions in C++?
|
View Answer
|
|
Can I create my own functions in C?
|
View Answer
|
|
How do I generate random numbers in Python?
|
View Answer
|
|
Are there any interfaces to database packages in Python?
|
View Answer
|
|
How do I avoid blocking in the connect() method of a socket?
|
View Answer
|
|
How do I send mail from a Python script?
|
View Answer
|
|
How can I mimic CGI form submission (METHOD=POST)?
|
View Answer
|
|
How do I run a subprocess with pipes connected to both input and output?
|
View Answer
|
|
How do I read (or write) binary data?
|
View Answer
|
|
How do I copy a file?
|
View Answer
|
|
How do I delete a file?
|
View Answer
|
|
How do I parcel out work among a bunch of worker threads?
|
View Answer
|
|
None of my threads seem to run: why?
|
View Answer
|
|
How do I test a Python program or component?
|
View Answer
|
|
Why don't my signal handlers work?
|
View Answer
|
|
How do I make a Python script executable on Unix?
|
View Answer
|
|
Where is the math.py (socket.py, regex.py, etc.) source file?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|