Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can not call method in parent class, when using nested classes with inheritance #1420

Closed
Labels
Milestone
@koliyo

Description

@koliyo

Environment

Pythonnet version: 2.5.1
Python version: Python 3.9.2
Operating System: MacOS
.NET Runtime: 5.0.200

Details

Related to the problem reported in#1414

I.e. classes-in-classes are not handled properly in Pythonnet.

Even when applying the workaround mentioned in the#1414 issue there are still problems related to the inheritance.

Namely, I can not call a method declared on the baseclass, on an instance of the nested child class.

The following code demonstrates the issue

namespacepythonnet_testing{publicclassBar{publicstringMethodInBaseClass(){return"hello";}publicclassHej:Bar{}}publicclassFoo{publicBarBar;publicstaticFooCreate(){Foof=new();f.Bar=newBar.Hej();returnf;}}classProgram{staticvoidMain(string[]args){varpyCode=@"import pythonnet_testingf = pythonnet_testing.Foo.Create()print('Trying to access member of nested subclass')_ = pythonnet_testing.Bar # NOTE: The python code crashes unless this explicit reference to the Bar class is usedprint(f'Bar: {f.Bar}')print(f'Method call: {f.Bar.MethodInBaseClass()}')      ";PythonEngine.Initialize();using(Py.GIL()){PythonEngine.Exec(pyCode);}}}

Running this gives the following error message

Trying to access member of nested subclassBar: pythonnet_testing.Bar+HejUnhandled exception. Python.Runtime.PythonException: AttributeError : 'Hej' object has no attribute 'MethodInBaseClass'['  File "<string>", line 7, in <module>\n']   at Python.Runtime.PythonException.ThrowIfIsNull(BorrowedReference reference)   at Python.Runtime.PythonEngine.RunString(String code, Nullable`1 globals, Nullable`1 locals, RunFlagType flag)

In order to call theMethodInBaseClass method on the child class, I must explicitly re-define it like this:

publicclassHej:Bar{publicnewstringMethodInBaseClass()=>base.MethodInBaseClass();}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp