Remove ObjectProtocol to make usage more Pythonic#892
Remove ObjectProtocol to make usage more Pythonic#892davidhewitt wants to merge 1 commit intoPyO3:masterfrom
Conversation
|
Thank you, but now I'm thinking about there can be a case we need |
👍 - IMHO (I guess these changes also affect |
|
FWIW I feel more strongly about the names like |
|
Regarding removing ObjectProtocol, I like the idea of having only one place where to look up all the available methods. About having |
|
That's a reasonable argument @Alexander-N 👍 Maybe here's another idea - what do you think if we supported both Then those like me who want to use these functions like in Python can write And also those like yourself who prefer the Rustic way can use I mostly agree with you there's an argument that "there should be only one obvious way" - but I think on this point the obvious way maybe differs if you're a Python user expecting to do |
|
I think as soon as several people work on one codebase there will be a cost in enabling inconsistencies which for me outweighs the benefit of having different APIs for different preferences. Also, I fear that this will cause confusion to new users, as to what's the difference between |
I think the idea here is that ObjectProtocol is just redundant since it would only be implemented for PyAny. So there's still only one place to look them up. |
|
Note that |
|
Since it is quite hard to see at a glance: the difference is that |
|
Strange! I wonder if this is a CPython implementation detail, or is specified in docs. I'll have a dig 😄 |
Please wait for a bit...
OK. No problem except it can look odd to use
The only concern I had was it can be separated into
As a user, I don't want them. And as a maintainer, I haven't heard that any user complains about that(e.g., we have no issue like 'why PyO3 doesn't have |
FWIW, I agree. I'm perfectly happy with methods, as long as the names and their correspondence are clear (e.g. If we want to introduce APIs that mimic the behavior of more complex built-in Python functions, they should probably be free functions as well. |
|
Ok I will give up on the |
|
I'll follow up with the simpler changes as a separate PR, for easier reviewing. |
This is a follow up to my comment in #886 . There's two ideas here that I've been thinking about for a while.
I'd be really interested to hear what you think of these ideas, and if you like them, I will finish off this PR according to feedback received.
ObjectProtocol isn't needed any more
get_super()some time ago, it's very nice ifPyListetc. implementDerefforPyAny. This is now very easy to do with New Native Types and Lighter GILPool #887 merged.Derefis implemented, I don't thinkObjectProtocolserves any special purpose not served by just putting the methods directly onPyAny. So I moved them there.PyAnyto see all the methods they can call. Also removing a trait makes less things for users to understand.PyAny) rather than for everyTwhich implementsObjectProtocol.More pythonic
repr(),str()etc.If I have an object
oin Python I can get itsreprby either doingrepr(o)oro.__repr__(). So in my opinion pyo3 should offer the same API.To allow this, I:
pyo3::builtin_methodswhich for the moment just hasrepr,str,lenandhash.PyAny::__repr__PyAny::repretc.After this change, I can now call either
pyo3::repr(o), oro.__repr__().TODO:
PyAnymethodspyo3::builtin_methodsgetattr,setattr,booletc. (https://docs.python.org/3/library/functions.html)