question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class instance a derived Python class or pure C#?

See original GitHub issue

Hey everybody, I am trying to see if there is a way to determine if my instance of a class is a pure C# class or a derived from C# Python class.

Here is an example:

Lets say I have a C# class public class Parent, -> in Python I create a subclass of it class PyClass(Parent), -> in C# I create a subclass of it public class CSharpClass : Parent

In both of these cases when I create the new objects it will use the Parent class constructor, but in that constructor I would like the behaviour to change if the instance is from a derived python class.

Is there a way to verify in the Parent class constructor that its being used in a derived Python class?

Thank you!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lostmsucommented, Oct 12, 2020

@C-SELLERS I was debugging some unrelated issue today, and came across ClassDerivedObject, that handles certain Python classes derived from C#.

First, is there a reason you don’t want to define your class as a CLR-like class? To do that you’d need to set __namespace__ like this:

class MyClass(Prototype.IProcessControl):
    __namespace__ = "Axa.Core.Prototype"

I believe in this case the derived class will implement dummy IPythonDerivedType.

0reactions
C-SELLERScommented, Oct 1, 2020

Just a heads up, if anybody tries using the above, instances will also contain a copy of the object your originally passed in. I realize this probably isn’t desired so I added one extra step that will go get the type first, then dispose of our converted Python obj.

        /// <summary>
        /// Method for getting Python instances of a given obj type.
        /// This includes all subclass instances of the obj type.
        /// </summary>
        /// <param name="obj">instance of the object type you want to collect</param>
        /// <returns>Python List of PyObjects</returns>
        private static PyObject GetPythonInstances(object obj)
        {
            using (Py.GIL())
            {
                // Passing Python a type, even when converted using ToPython(), does not actually convert to Python type
                // so we must go fetch the type from Python, then dispose of this object so we don't get it in our list of
                // instances.
                var PythonObject = PyObject.FromManagedObject(obj);
                var type = PythonEngine.ModuleFromString(
                    "getPyType",
                    "def get_type(obj):\n" +
                    "   return type(obj)"
                ).InvokeMethod("get_type", PythonObject);
                PythonObject.Dispose();

                // Get all PyObjects that are instances of the type determined
                var instances = PythonEngine.ModuleFromString(
                    "getObject",
                    "import gc\n" +
                    "def get_instances(objType):\n" +
                    "   return [x for x in gc.get_objects() if isinstance(x, objType)]"
                ).InvokeMethod("get_instances", type);

                return instances;
            }
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cast base class to derived class python (or more ...
This is as close to a "cast" as you can get in Python, and like casting in C, it is not to be...
Read more >
9. Classes — Python 3.11.4 documentation
Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class ......
Read more >
Python issubclass()
It is the capability of one class to derive or inherit the properties from some other class. It also provides the reusability of...
Read more >
Python Classes: The Power of Object-Oriented Programming
In this tutorial, you'll learn how to create and use full-featured classes in your Python code. Classes provide a great way to solve...
Read more >
Subclassing and Inheritance — Programming in Python 7.0 ...
Classes can inherit attributes and behavior from pre-existing classes called base classes or super classes. The resulting classes are known as derived ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found