Skip to content

[py 3.5] Instantiating subclass throws exception: TypeError: no constructor matches given arguments #252

Closed

Description

@Thraka

I'm new to this

I'm using Python 3.5

I have my own .NET library class that does not have a parameterless constructor. I want to subclass it in python and create a parameterless constructor. However, python always throws an error when I do not specify parameters on creating an instance of the class. The parameters for the C# class constructor is int, int.

sub class

class TestConsole(SadConsole.Consoles.Console):

    def __init__(self):
        super().__init__(80, 25)
        super().Print(0, 0, "right constructor")

    def __init__(self, x, y):
        super().__init__(x, y)
        super().Print(0, 0, "wrong constructor")

calling code that crashes with error

new_console = TestConsole()

force parameters, it works fine, but obviously prints out "wrong constructor"

new_console = TestConsole(80, 25)

Activity

  1. den-run-ai commented on Aug 31, 2016

    @den-run-ai
    Contributor

    Can you share minimum reproducible SadConsole.Consoles.Console?

    There is an open pull request about overloading on constructor and also
    subclassing a python class that inherits from .NET class is buggy (see
    mailing list).

    On Tuesday, August 30, 2016, Andy De George [email protected]
    wrote:

    I'm new to this

    I'm using Python 3.5

    I have my own .NET library class that does not have a parameterless
    constructor. I want to subclass it in python and create a parameterless
    constructor. However, python always throws an error when I do not specify
    parameters on creating an instance of the class. The parameters for the C#
    class constructor is int, int.

    sub class

    class TestConsole(SadConsole.Consoles.Console):

    def __init__(self):
        super().__init__(80, 25)
        super().Print(0, 0, "right constructor")
    
    def __init__(self, x, y):
        super().__init__(x, y)
        super().Print(0, 0, "wrong constructor")
    

    calling code that crashes with error

    new_console = TestConsole()

    force parameters, it works fine, but obviously prints out "wrong
    constructor"

    new_console = TestConsole(80, 25)

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly, view it on GitHub
    #252, or mute the thread
    https://github.com/notifications/unsubscribe-auth/AHgZ5akGGnk_ml70oaHmLhZcAQzIMBfuks5qlMxEgaJpZM4JxHgz
    .

  2. Thraka commented on Aug 31, 2016

    @Thraka
    Author

    class-test.zip

    Here you go. Very simple test with C# code provided.

  3. den-run-ai commented on Aug 31, 2016

    @den-run-ai
    Contributor

    This is error in both pythonnet and ironpython:

    image

    image

  4. Thraka commented on Aug 31, 2016

    @Thraka
    Author

    Is this a scenario we can cover some how? It seems that a .NET type as a base class, the .NET base class gets initialized before the Python inherited class, which is opposite to how it seems it should work.

  5. den-run-ai commented on Aug 31, 2016

    @den-run-ai
    Contributor

    Looking at history this limitation was originally like that in pythonnet.

    And some updates on subclassing more recently here from @tonyroberts with
    so many limitations listed:

    http://code.activestate.com/lists/pythondotnet/1397/

    So have you looked at using python closures? You would have your
    alternative constructor logic in the outer function scope and use all .NET
    methods in the inner scope.

    On Wed, Aug 31, 2016 at 2:05 PM, Andy De George [email protected]
    wrote:

    Is this a scenario we can cover some how? It seems that a .NET type as a
    base class, the .NET base class gets initialized before the Python
    inherited class, which is opposite to how it seems it should work.

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #252 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AHgZ5VFwgH9WhFlu7ghboMpRh_cJOBasks5qldBigaJpZM4JxHgz
    .

  6. den-run-ai commented on Sep 20, 2016

    @den-run-ai
    Contributor

    @Thraka you could just pass 2 dynamic objects (dyn_method and dyn_args) to constructor in C# not used on .NET side if null. Then just pass these dynamic object arguments to constructor on Python side to pass the python method and python arguments.

  7. den-run-ai commented on Sep 20, 2016

    @den-run-ai
    Contributor
    namespace PythonTest
    {
        public class Person
        {
            public string Name;
            public Person (string newName, dynamic dyn_method, dynamic dyn_args)
            {
                Name = newName;
                if (dyn_method != null)
                {
                    dyn_method(dyn_args);
                }
            }
        }
    }
  8. filmor commented on Jun 9, 2020

    @filmor
    Member

    Since there have not been any updates in quite a while, I'll close this issue. Feel free to reopen and provide a information on the current state.

  9. angel88c commented on May 9, 2022

    @angel88c

    What's happen when the .Net constructor class returns a out string inside the parameters?

    ` public TestClass(string name, out string error)
    {
    error = "Error in the Initialization";
    _name = name;

        }`
    

    How can I get the out string parameter from python in the instance of object? I've tried using tuples and have error TypeError: iteration over non-sequence

    error ='' obj, error = TestClass("Angel", '') print(obj)

    Thanks

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