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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for dunder methods to call their JS equivalent consistently #4037
Comments
Can you please give an example of code you wrote, what it currently does, and what you would prefer for it to do? |
I have a python DateRange Class that I want to be able to check other dates membership inside the range. So I made the following dunder method in python:
but when I run the includes method on an instance of the object in Javascript with a JS Date object as the method's argument, I get a KeyError rather than running my python method. What I was expecting was that it would work like using the |
Well let a = pyodide.runPython(`
class A:
def __repr__(self):
return "a"
A()
`);
a.toString() I don't recall why we picked For
If you register it as a
This is meant to line up with the way JavaScript objects work: there is |
To the toString method using the I don't know how you're running your pyodide code, but I'm running it through TS-Node and adding |
Oh yes, the Looking at the history, it seems that Since then I'm not sure if anyone has discussed whether it should do |
Ahh that makes sense for the example. Regardless, it seems that the separation between includes and has in Javascript doesn't apply to python in any sensible way, so I'll leave it at what it is. I've given my case for |
I'll open a separate issue and solicit some opinions. |
Okay I'm going to close this issue @Duncanr-glitch but feel free to reopen it if you think it hasn't been resolved, or open a new issue if you have further questions. |
馃殌 Feature
If contains or str (or any dunder method, these are just the 2 I ran into personally) are defined in a class, when the includes and toString methods are invoked on instances of that class on the Javascript side, these methods should be run. Currently only the Javascript implementation runs.
However, these functions do work if you call them directly (i.e. cls.contains(value))
Surprisingly, if you get the length property, it does call the len method in the class.
Motivation
Dunder methods are a powerful means of class functionality in python.
Pitch
If a dev has to call method explicitly when there's a JS equivalent to the python operator, then it makes the code less clean and harder to maintain since it makes the code both less pythonic and less natively Javascript-like.
The text was updated successfully, but these errors were encountered: