|
Technical Interview Questions
C++ Interview Questions
Php Interview Questions
Xml
Interview Questions
C Interview Questions
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Python Interview Questions and Answers
How do I extract C values from a Python object?
That depends on the object's type. If it's a tuple,
PyTupleSize(o) returns its length and PyTuple_GetItem(o,
i) returns its i'th item. Lists have similar functions,
PyListSize(o) and PyList_GetItem(o, i).
For strings, PyString_Size(o) returns its length and
PyString_AsString(o) a pointer to its value. Note that
Python strings may contain null bytes so C's strlen()
should not be used.
To test the type of an object, first make sure it isn't
NULL, and then use PyString_Check(o), PyTuple_Check(o),
PyList_Check(o), etc.
There is also a high-level API to Python objects which
is provided by the so-called 'abstract' interface --
read Include/abstract.h for further details. It allows
interfacing with any kind of Python sequence using calls
like PySequence_Length(), PySequence_GetItem(), etc.) as
well as many other useful protocols.
How do I call an object's method from C?
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)".
How do I catch the output from PyErr_Print() (or
anything that prints to stdout/stderr)?
In Python code, define an object that supports the
write() method. Assign this object to sys.stdout and
sys.stderr. Call print_error, or just allow the standard
traceback mechanism to work. Then, the output will go
wherever your write() method sends it.
The easiest way to do this is to use the StringIO class
in the standard library.
Sample code and use for catching stdout:
>>> class StdoutCatcher:
... def __init__(self):
... self.data = ''
... def write(self, stuff):
... self.data = self.data + stuff
...
>>> import sys
>>> sys.stdout = StdoutCatcher()
>>> print 'foo'
>>> print 'hello world!'
>>> sys.stderr.write(sys.stdout.data)
foo
hello world!
How do I access a module written in Python from C?
You can get a pointer to the module object as follows:
module = PyImport_ImportModule("<modulename>");
If the module hasn't been imported yet (i.e. it is not
yet present in sys.modules), this initializes the
module; otherwise it simply returns the value of
sys.modules["<modulename>"]. Note that it doesn't enter
the module into any namespace -- it only ensures it has
been initialized and is stored in sys.modules.
You can then access the module's attributes (i.e. any
name defined in the module) as follows:
attr = PyObject_GetAttrString(module, "<attrname>");
Calling PyObject_SetAttrString() to assign to variables
in the module also works.
How do I interface to C++ objects from Python?
Depending on your requirements, there are many
approaches. To do this manually, begin by reading the
"Extending and Embedding" document. Realize that for the
Python run-time system, there isn't a whole lot of
difference between C and C++ -- so the strategy of
building a new Python type around a C structure
(pointer) type will also work for C++ objects.
Page Numbers : 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
HTML Interview
Questions for more HTML Interview Questions with Answers
Check
Job Interview Questions
for more Interview Questions with Answers
|