How to implement __repr__ for a C# class to be used in python.net?

752 views Asked by At

I have pythonnet v 2.3.0 installed. I have a C# class library which I use in Python. I have a Point class (a code snippet):

public Point(double x, double y)
{
    this.x = x;
    this.y = y;
}

public override string ToString()
{
    return $"Point({x}, {y})";
}

When I work with this class library in Python (in Python shell):

>>> p1 = Point(10,15)
>>> p1
<Geometry.Point object at 0x000000000558F828>
>>> str(p1)
'Point(10, 15)'

Because I override the ToString(), when I run str(p1) I see my custom view of the object. However, when I just access a variable p1, then it goes to the __repr__() method under the hood which gives me ugly <Geometry.Point object at 0x000000000558F828>.

Is there a method in C# class that is possible to override so that pythonnet would use that method when getting the __repr__?

https://github.com/pythonnet/pythonnet/issues/680

0

There are 0 answers