FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Python.Runtime.Converter support for converting from/to PyObject · Issue #296 · pythonnet/pythonnet · GitHub

Repository navigation

Python.Runtime.Converter support for converting from/to PyObject #296

Closed

Description

I have a C# class, lets call it MyCSClass, that I intend to be inherited from a Python class and used like this:

class MyPythonClass(MyCSClass):
   def __init__(self):
      self.MyAttribute = 5
      self.Callback(self)

When an instance of MyPythonClass gets passed to C#, it gets converted to a MyCSClass and is stripped of all the attributes. Even if the method called in CS takes it as a dynamic or PyObject. An example of this could be the init method above calling into the following C# code:

public class MyCSClass {
   protected void Callback(dynamic self){
       Console.WriteLine("A dynamic attribute: {0}", self.MyAttribute);
   }
}

However, the instance assigned to 'self' is an MyCSClass and not a dynamic MyPythonClass. Replace dynamic with PyObject and you get an exception. If suggest the the converter should support PyObject, so that callbacks like these are made possible.

Then I would have to do this instead, which is OK for me:

public class MyCSClass {
   protected void Callback(PyObject self){
       Console.WriteLine("A dynamic attribute: {0}", ((dynamic)self).MyAttribute);
   }
}

Python Version: Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
OS: Windows 7 Enterprise

Activity

  1. changed the title [-]Converter support for converting from/to PyObject[/-] [+]Python.Runtime.Converter support for converting from/to PyObject[/+] on Nov 29, 2016
  2. den-run-ai commented on Nov 29, 2016

    Contributor
  3. rmadsen-ks commented on Nov 29, 2016

    ContributorAuthor

    In the specific case, __init__ is setting a (non-virtual) property of the base class, but I can confirm that it also happens outside of __init__.

    I do this:

    class MyPythonClass(MyCSClass):
       def __init__(self):
          self.MyAttribute = 5
       def run(self):
          self.Callback(self)

    I get the converted MyCSClass instance inside self.Callback, if the argument is dynamic. If I instead choose PyObject as the type of the argument I just get the following error:

    'TypeError : No method matches given arguments'.

  4. rmadsen-ks commented on Nov 29, 2016

    ContributorAuthor

    Note pull request #297

  5. den-run-ai commented on Nov 29, 2016

    Contributor

    here is related issue with some tips from @tonyroberts:

    #94

    #94 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL