Skip to content

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

Description

@AlexArcPy

Environment

  • Pythonnet version: 2.3.0
  • Python version: 2.7
  • Operating System: Windows 10

Details

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__?

Activity

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