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

Appearance settings

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

Copy link
Copy link
@AlexArcPy

Description

@AlexArcPy
Issue body actions

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

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.