[Python.NET] Python class instance from C#

Denis Akhiyarov denis.akhiyarov at gmail.com
Tue Jan 6 15:53:18 CET 2015

Does anyone have an example of using Python.NET in embedding scenario for
creating an instance of a Python class/object, possibly with arguments
passed to __init__() and then using this instantiated class to call its
The workaround for me right now is calling Python wrapper function that
returns new instance of this class.

I have an example that does NOT work for me:

        IntPtr pyobj = default(IntPtr);
        IntPtr u_name = Runtime.PyUnicode_FromString(ModuleName);
        IntPtr module = Runtime.PyImport_Import(u_name);
        IntPtr pyclass = Runtime.PyObject_GetAttrString(module, ObjectName);

        if (Runtime.PyCallable_Check(pyclass) == 1)
            pyobj = Runtime.PyObject_CallObject(pyclass, pyargtuple);

        IntPtr inst = Runtime.PyObject_Call(pyobj, IntPtr.Zero);

        PyObject result = new PyObject(inst);

        return result;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20150106/e540978f/attachment.html>

More information about the PythonDotNet mailing list