-
Notifications
You must be signed in to change notification settings - Fork 748
Method precedence #552
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
Comments
Can you explicitly use .Overloads on your methods to enforce the correct
overload?
…On Wed, Oct 4, 2017, 4:04 PM Alexandre Catarino ***@***.***> wrote:
Environment
- Pythonnet version: 2.4.0
- Python version: 2.7
- Operating System: Windows 10
Details
We have a series of overloads for a given method in existing base class
and we want to add new overloads that should take precedence. To be more
precise, we have the following case:
public IEnumerable<Slice> History(int periods)
that is consumed by C# classes and we have
public PyObject History(int periods)
that is consumed by Python classes
To achieve it, we have created a subclass where the second overload is
defined:
public class QCAlgorithm
{
public IEnumerable<Slice> History(int periods)
{
}
}
public class QCPyAlgorithm : QCAlgorithm
{
public new PyObject History(int periods)
{
}
}
When we use reflection to get the methods from QCPyAlgorithm, the new
method comes first. However, after the sorting performed by pythonnet here
<https://github.com/pythonnet/pythonnet/blob/master/src/runtime/methodbinder.cs#L162>,
the method from QCAlgorithm comes first. Consequently it is called in my
python script and returns an enumerable of Slice.
*The issue/question is:*
How do I keep the methods from the declaring type on the top of the list
of methods are try to bind?
My first guess is to send the first overload behind by adding the
following line in the GetPrecedence
<https://github.com/pythonnet/pythonnet/blob/master/src/runtime/methodbinder.cs#L183>
method:
val += mi.DeclaringType == mi.ReflectedType ? 0 : 3000;
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#552>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHgZ5Xii8t9l0_2FXcP5AD4QZ5MKmqUTks5so_LVgaJpZM4PuPCp>
.
|
In our particular case (QuantConnect/Lean), we want users coding in C# and Python to use the exact same method call. We don't want them to learn all the overloads or use a different method signature. |
You can fix the method sorting by not trying to do it via a mere I usually don't expose the .NET objects directly to the user and instead wrap it in a more pythonic library, essentially using Python.NET as an FFI, that's another option. |
fix merged |
@filmor I'm interested in using Python.NET as an FFI, do you have any resources for doing so, or should I refer to your repo https://github.com/filmor/clr-loader/ as an example of what you meant? Thank you! |
@christabella This is unrelated to clr-loader. What I meant is that I usually don't expose the objects directly to the users, e.g.: public class SomeClass {
public int SomeMethod(string param) { ... }
} from My.Dotnet.Library import SomeClass as ClrSomeClass
class SomeClass:
def __init__(self):
self._clr_instance = ClrSomeClass()
def some_method(self, param):
return self._clr_instance.SomeMethod(param) |
@filmor thank you for the illuminating example, this looks like a great solution for keeping the Python interface clean while using Python.NET to load CLR namespaces as Python packages! Just in case, do you have any examples of this pattern in action? (I was looking at QuantConnect/Lean repo mentioned earlier in this thread but maybe it's not exactly what you meant) |
Environment
Details
We have a series of overloads for a given method in existing base class and we want to add new overloads that should take precedence. To be more precise, we have the following case:
public IEnumerable<Slice> History(int periods)
that is consumed by C# classes and we have
public PyObject History(int periods)
that is consumed by Python classes
To achieve it, we have created a subclass where the second overload is defined:
When we use reflection to get the methods from QCPyAlgorithm, the new method comes first. However, after the sorting performed by pythonnet here, the method from QCAlgorithm comes first. Consequently it is called in my python script and returns an enumerable of Slice.
The issue/question is:
How do I keep the methods from the declaring type on the top of the list of methods are try to bind?
My first guess is to send the first overload behind by adding the following line in the GetPrecedence method:
The text was updated successfully, but these errors were encountered: