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

Python grandchild of .NET class is not created correctly if "__namespace__" attribute is set · Issue #544 · pythonnet/pythonnet · GitHub

Repository navigation

Python grandchild of .NET class is not created correctly if "__namespace__" attribute is set #544

Closed

Description

Environment

Details

  • Describe what you were trying to get done.

I am trying to use python child of .NET class in python grandchild.

class A:
  def __init__(self):
      self.foo()
  def foo(self): print('A.foo')

class B(A):
  def foo(self): print('B.foo')

a = A()
b = B()

works like expected and prints:

A.foo
B.foo

Consider Python classes constructed with pythonnet:

import clr
import System.Windows.Forms as WinForms

class PythonGroupBox(WinForms.GroupBox):
    def __init__(self):
        print("PythonGroupBox.__init__")
        self.MyInit()

    def MyInit(self):
        print("PythonGroupBox MyInit")

class PythonGroupBoxDerived(PythonGroupBox):
    def MyInit(self):
        print("PythonGroupBoxDerived MyInit")

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

works fine and prints:

PythonGroupBox.init
PythonGroupBox MyInit
PPythonGroupBox.init
PythonGroupBoxDerived MyInit

Now the same with "namespace":

import clr
import System.Windows.Forms as WinForms

class PythonGroupBox(WinForms.GroupBox):
    __namespace__ = "System.Windows.Forms"
    def __init__(self):
        print("PythonGroupBox.__init__")
        self.MyInit()

    def MyInit(self):
        print("PythonGroupBox MyInit")

class PythonGroupBoxDerived(PythonGroupBox):
    def MyInit(self):
        print("PythonGroupBoxDerived MyInit")

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

The output is:

PythonGroupBox.init
PythonGroupBox MyInit
PythonGroupBox.init
PythonGroupBox MyInit

Error (1), because MyInit Method didn't get overloaded !!!

One more try:

import clr
import System.Windows.Forms as WinForms

class PythonGroupBox(WinForms.GroupBox):
    __namespace__ = "System.Windows.Forms"
    def __init__(self):
        print("PythonGroupBox.__init__")
        self.MyInit()

    def MyInit(self):
        print("PythonGroupBox MyInit")

class PythonGroupBoxDerived(PythonGroupBox):
    __namespace__ = "System.Windows.Forms"
    def MyInit(self):
        print("PythonGroupBoxDerived MyInit")

grp1 = PythonGroupBox()
grp2 = PythonGroupBoxDerived()

Output gets really weird:

PythonGroupBox.init
PythonGroupBox MyInit

Error (2): during construction of PythonGroupBoxDerived the init() method was not called at all !!!

Important: this issue has nothing to do with issue #503: if I undo that fix than constructors get called twice, but errors described here persist.

Activity

  1. changed the title [-]Python granchild of .NET class is not created correctly if "__namespace__" attribute is set[/-] [+]Python grandchild of .NET class is not created correctly if "__namespace__" attribute is set[/+] on Sep 15, 2017
  2. testrunner123 commented on Sep 20, 2017

    ContributorAuthor

    I have made a unit test for this on a branch. Does some one knows how it is designed to work?

  3. lostmsu commented on Sep 17, 2022

    Member

    This works as expected:

    import System.Windows.Forms as WinForms
    
    class PythonGroupBox(WinForms.GroupBox):
        __namespace__ = "System.Windows.Forms"
        def __init__(self):
            super().__init__()
            print("PythonGroupBox.__init__")
            self.MyInit()
        def MyInit(self):
            print("PythonGroupBox MyInit")
    
    class PythonGroupBoxDerived(PythonGroupBox):
        __namespace__ = "System.Windows.Forms"
        def __init__(self):
            super().__init__()
        def MyInit(self):
            print("PythonGroupBoxDerived MyInit")
    
    grp1 = PythonGroupBox()
    grp2 = PythonGroupBoxDerived()

    / grp1 = PythonGroupBox()
    PythonGroupBox.init
    PythonGroupBox MyInit
    / grp2 = PythonGroupBoxDerived()
    PythonGroupBox.init
    PythonGroupBoxDerived MyInit

    However, without explicitly redeclared __init__ in the PythonGroupBoxDerived base constructor is not called, for which I opened a separate issue: #1945

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