Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix whitespace
  • Loading branch information
koubaa committed Sep 15, 2019
commit 2fb6a430a0eb4f271cfdc9229bbe55ac8bc5b85e
10 changes: 5 additions & 5 deletions src/runtime/classbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ public static IntPtr tp_str(IntPtr ob)

//First check which type in the object hierarchy provides ToString()
//ToString has two "official" overloads so loop over GetMethods to get the one without parameters
var method = type.GetMethod("ToString", new Type[]{});
if (method.DeclaringTyppe != typeof(object))
{
var method = type.GetMethod("ToString", new Type[]{});
if (method.DeclaringTyppe != typeof(object))
{
//match! something other than object provides a parameter-less overload of ToString
return Runtime.Pystring_FromString(co.inst.ToString());
}
return Runtime.Pystring_FromString(co.inst.ToString());
}

//If the object defines __repr__, call it.
System.Reflection.MethodInfo reprMethodInfo = instType.GetMethod("__repr__");
Copy link
Copy Markdown
Member

@lostmsu lostmsu Sep 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all .NET types have ToString, why does it even try __repr__? If somebody really needs it, they can do override string ToString() => __repr__()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the same, though. ToString is more akin to __str__, there is no real equivalent to __repr__ in .NET.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the tp_str implementation. tp_repr is below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu See the comment:
//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.

This sentence comes straight from the python manual, I didn't make it up

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koubaa the default inheritance hierarchy for C# classes from Python's point of view is Python's object <- System.Object <- CustomCSharpClass.

Since for all .NET classes overriding ToString is how you implement __str__, I don't see why it should be different for System.Object. Hence I think .NET classes should just always call ToString for tp_str.

Copy link
Copy Markdown
Contributor Author

@koubaa koubaa Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu I took a look. I agree some kind of consensus between interface vs reflection for dunder methods should be reached and applied throughout the code base. One argument for reflection is that you can use extension methods to add these. If I take an existing C# codebase and write some extension methods in a separate assembly, I can provide __repr__ and __getattr__ to it. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koubaa, extension methods would be hard to find. You'd have to enumerate all types in all assemblies. GetMethods won't return them.

In addition to that, what would be the semantics of multiple assemblies defining an extension method with the same signature?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu ah, didn't know that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu I am still not convinced about the interface approach. It would require a hard build dependency on C# libraries onto PythonNet. It seems like a tough sell for some of the mature C# libraries out there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu I changed this as you requested. I don't think I can capture the repr function in a Func object since this is a static method.

Expand Down