[Python.NET] Error in calling Python class method from C#

techi eth techieth at gmail.com
Mon Apr 30 04:47:33 EDT 2018


I have got success on doing. I just  need to get review to ensure right.
Please find below code snapshot.

Class.py

def test():
 print "Test called"
 return 1

class ClassTest:
  def __init__(self)

  def Initalize(self):
   print "Initalize called"
   return 1

  @staticmethod
  def Execute():
   print "Execute called"
   return 1
-------------------------------------------------------------------------------------
Programme.cs
 PythonEngine.Initialize();
            using (Py.GIL())
            {
                //Creating module object
                PyObject testClassModule =
PythonEngine.ImportModule("Class");

                //Calling module method
                PyObject result = testClassModule.InvokeMethod("test");
                Console.WriteLine("Test method result = {0}",
result.ToString());

                Dynamic classTest = testClassModule.GetAttr("ClassTest");
Dynamic tmp = classTest();
                tmp.Initalize();
                tmp.Execute();
           }
            PythonEngine.Shutdown();
            return 0;
        }

On Mon, Apr 30, 2018 at 11:35 AM, techi eth <techieth at gmail.com> wrote:

> Iron Python is having way to do the same.Please check below link.
> https://stackoverflow.com/questions/579272/instantiating-a-python-class-
> in-c-sharp
>
>
>
>
>
> On Mon, Apr 30, 2018 at 10:36 AM, techi eth <techieth at gmail.com> wrote:
>
>> Thanks for link, I have been through most of pages but not able to found
>> same query.
>> I am also not sure is this possible with Pyhton For .NET. Could you
>> please give me below query answer.
>>
>> Can i create python class instance in C# programme ????
>>
>> On Mon, Apr 30, 2018 at 8:34 AM, Denis Akhiyarov <
>> denis.akhiyarov at gmail.com> wrote:
>>
>>> You may get more feedback on stackoverflow than on this mailing list for
>>> questions like this:
>>>
>>> https://stackoverflow.com/questions/tagged/python.net?sort=n
>>> ewest&pageSize=50
>>>
>>> On Fri, Apr 27, 2018 at 3:54 AM, techi eth <techieth at gmail.com> wrote:
>>>
>>>> Any input on raised query. I think i am missing something very simple
>>>> but not able to catch.
>>>>
>>>> Thanks
>>>>
>>>> On Thu, Apr 26, 2018 at 5:47 PM, techi eth <techieth at gmail.com> wrote:
>>>>
>>>>> Yes,It will work with this way but i need to create multiple instance
>>>>> of same class.If you see other function (@Execute() ) with static was
>>>>> working.
>>>>> Please give me hint of doing without static.
>>>>>
>>>>> Thanks
>>>>>
>>>>> On Thu, Apr 26, 2018 at 4:54 PM, Stefan Schukat <SSchukat at dspace.de>
>>>>> wrote:
>>>>>
>>>>>> Hello,
>>>>>>
>>>>>>
>>>>>>
>>>>>> you missed either @staticmethod decorator or the self argument for
>>>>>> the Initialize method.
>>>>>>
>>>>>>
>>>>>>
>>>>>>     Stefan
>>>>>>
>>>>>>
>>>>>>
>>>>>> *From:* PythonDotNet [mailto:pythondotnet-bounces+sschukat=
>>>>>> dspace.de at python.org] *On Behalf Of *techi eth
>>>>>> *Se**nt:* Wednesday, April 25, 2018 2:38 PM
>>>>>> *To:* A list for users and developers of Python for .NET <
>>>>>> pythondotnet at python.org>
>>>>>> *Subject:* [Python.NET] Error in calling Python class method from C#
>>>>>>
>>>>>>
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>>
>>>>>>
>>>>>> I am facing issue while calling python class function in C#
>>>>>> programme. Can anyone give me hint what is wrong in below programme.While
>>>>>> running I am getting error in invoking Initialize() function of class.
>>>>>>
>>>>>>
>>>>>>
>>>>>> Python module <Class.py> :
>>>>>>
>>>>>> def test():
>>>>>>
>>>>>>  print "Test called"
>>>>>>
>>>>>>  return 1
>>>>>>
>>>>>>
>>>>>>
>>>>>> class ClassTest:
>>>>>>
>>>>>>   def __init__(self)
>>>>>>
>>>>>>
>>>>>>
>>>>>>   def Initialize ():
>>>>>>
>>>>>>    print " Initialize called"
>>>>>>
>>>>>>    return 1
>>>>>>
>>>>>>
>>>>>>
>>>>>>   @staticmethod
>>>>>>
>>>>>>   def Execute():
>>>>>>
>>>>>>    print "Execute called"
>>>>>>
>>>>>>    return 1
>>>>>>
>>>>>>
>>>>>>
>>>>>> C# Console Programme code snapshot :
>>>>>>
>>>>>> PythonEngine.Initialize();
>>>>>>
>>>>>>             using (Py.GIL())
>>>>>>
>>>>>>             {
>>>>>>
>>>>>>                 PyObject testClassModule =
>>>>>> PythonEngine.ImportModule("Class");
>>>>>>
>>>>>>
>>>>>>
>>>>>>                 //Calling module method
>>>>>>
>>>>>>                 PyObject result = testClassModule.InvokeMethod("
>>>>>> test");
>>>>>>
>>>>>>                 Console.WriteLine("Test method result = {0}",
>>>>>> result.ToString());
>>>>>>
>>>>>>
>>>>>>
>>>>>>                 PyObject classTest = testClassModule.GetAttr("Class
>>>>>> Test");
>>>>>>
>>>>>>                 *classTest.InvokeMethod("Initialize ");
>>>>>> -----------------------------> Error*
>>>>>>
>>>>>>                 classTest.InvokeMethod("Execute");
>>>>>>
>>>>>>            }
>>>>>>
>>>>>>             PythonEngine.Shutdown();
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> _________________________________________________
>>>>>> Python.NET mailing list - PythonDotNet at python.org
>>>>>> https://mail.python.org/mailman/listinfo/pythondotnet
>>>>>>
>>>>>>
>>>>>
>>>>
>>>> _________________________________________________
>>>> Python.NET mailing list - PythonDotNet at python.org
>>>> https://mail.python.org/mailman/listinfo/pythondotnet
>>>>
>>>>
>>>
>>> _________________________________________________
>>> Python.NET mailing list - PythonDotNet at python.org
>>> https://mail.python.org/mailman/listinfo/pythondotnet
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20180430/3e7adbe3/attachment.html>


More information about the PythonDotNet mailing list