Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AlexArcPy opened this issue Jun 3, 2018 · 10 comments
Closed

Comments

@AlexArcPy
Copy link

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

@den-run-ai
Copy link
Contributor

den-run-ai commented Jun 3, 2018

@AlexArcPy have you tried implementing Repr() on your C# class, it should be available here:

public string Repr()

public override string ToString()

@AlexArcPy
Copy link
Author

AlexArcPy commented Jun 3, 2018

@denfromufa , thanks for the comment. I've just tried, but to no effect.

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

In a Python IDE shell:

>>> p1
<Geometry.Point object at 0x00000000056A1550>
>>> p1.Repr()
u'Point(10, 15)'

In a cmd-based Python interpreter:

>>> p1 = Point(10,15)
>>> p1
<Geometry.Point object at 0x0000000005C44400>
>>> p1.Repr()
u'Point(10, 15)'
>>> p1.__repr__()
'<Geometry.Point object at 0x0000000005C44400>'

Should I be implementing this method in any kind of special way?

@den-run-ai
Copy link
Contributor

@AlexArcPy looks like i was wrong, i'm not sure how this is possible to implement.

@den-run-ai
Copy link
Contributor

@AlexArcPy ok, i took a second look and the first step is to find out which tp_repr method is called in your case using VS C# debugger:

https://github.com/pythonnet/pythonnet/search?p=1&q=tp_repr&unscoped_q=tp_repr

After that we can tell if it is possible to implement __repr__ on managed types without modifying pythonnet source code.

@AlexArcPy
Copy link
Author

@denfromufa , thanks a lot for digging into. This is definitely not a top priority feature to work on, so no stress! Should you find something more - please post! Thanks!

@koubaa
Copy link
Contributor

koubaa commented Jan 20, 2019

I've got a working implementation (it uses reflection to call __repr__ if one is defined) but I don't know how to fallback to the default if a __repr__ is not defined.

In standard CPython its done via object.__repr__(inst) but I'm not familiar enough with pythonnet to know how to get a reference to "class object"

Also, the behavior according to python documentation is this:
The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

Do we want this in pythonnet too?

@koubaa
Copy link
Contributor

koubaa commented Jan 20, 2019

I was able to figure it out. I'll make a pull request with my changes (including the behavior from the python doc I mentioned above).

@koubaa
Copy link
Contributor

koubaa commented Jan 20, 2019

#808

@den-run-ai
Copy link
Contributor

@koubaa thanks for this PR, let's get the CI tests passed.

@filmor filmor removed the help wanted label Jun 9, 2020
@filmor filmor closed this as completed Aug 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants