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
New function to get attribute without binding and use it instead of __func__ #25179
Comments
comment:1
Hi Jeroen, I agree that the idiom is not great :-) Do you have a way to implement your proposition without having to specify a super class or metaclass in each and every |
comment:2
Replying to @nthiery:
I don't think so. My idea would be to replace
by
Of course, the current code works so it's not required to do that change everywhere. I would suggest to do the change only when needed. |
Commit: |
comment:5
These are the minimal changes needed to get rid of the existing |
Author: Jeroen Demeyer |
comment:6
Thanks for investigating this. I definitely see the point of trying to improve the idiom which expose an implementation detail about bound/unbound methods. However I am reluctant with the current solution. Somehow it's too unlocal and non explicit. The risk is that a developper will see the idiom (without I would be more in favor of a local and explicit idiom like:
or
where we would use What do you think? Opinions anyone else? |
comment:7
Replying to @nthiery:
This wouldn't simply because you cannot "unbind" arbitrary objects. Imagine
There is no way to unbind
|
comment:8
I'll try to implement that on a different branch. |
This comment has been minimized.
This comment has been minimized.
comment:15
This is neat, but it seems like extreme overkill for a relatively minor issue. Were there any other use cases for this you had in mind? Because the current use case is handled much more simply by other means. |
comment:16
Replying to @jdemeyer:
I'm not sure what you mean by this. While I'm not particularly fond of the solution in #24955 either, it's not clear how this is any better, much less simpler. |
comment:17
I'm also -1 on immense mucking around with Python interpreter internals for seemingly little need/benefit. Although I agree it may be a useful capability to have and might be worth proposing upstream. As a different way of replacing the current idiom for copying methods from another class's |
comment:29
I think my problem just comes down to how I read "binding the attribute to the object" (and maybe it's my reading that's wrong). I think I would have worded this just subtly differently like "Like |
comment:30
Replying to @embray:
I guess my own reading is influenced by the first line of the
No idea what the average reading will be though.
Sounds good to me! |
comment:31
Replying to @embray:
Suppose |
comment:32
Replying to @embray:
I don't think that the implementation is complicated at all. I actually think it would be more complicated in pure Python since pure Python has no equivalent for Also note that most of the complications come from the support for old-style instances and classes. Once you remove that, it's basically three lines of code. |
comment:33
Replying to @embray:
Do you mean that the functionality of |
comment:35
Renamed |
comment:36
Replying to @jdemeyer:
Sorry, yes, I should clarify "complicated". In part I mean that it's implemented in C, though I don't believe that alone makes it complicated. I'm more concerned about the over-reliance on undocumented internals. I could just as easily write this function in pure Python, and I would be interested in a performance comparison between a pure python version and this C version, but I could try doing that myself. To be clear I'm otherwise +1 on this--that's my only misgiving at this point. |
comment:37
FYI - I have a new use-case for this on #25146 where I want to treat functions as class-level attributes, but Python by default binds them as methods. |
comment:38
Replying to @embray:
Well, the internals of old-style instances and classes aren't likely going to change in Python 2.7.16. And |
comment:39
I am starting to think that a Advantages:
The main disadvantage is that it would be more complicated to implement and slower. |
comment:40
For Python 3.3+ (and I think for practical purposes we're not interested in Python versions below 3.3) it looks like you want to use PyObject_GenericGetDict. This does practically the same thing you're doing with For Python 2.7 I agree it should be safe-enough since it's not going to change. As far as |
comment:41
I like the idea of the |
comment:42
Replying to @embray:
Did you forget that 2.7 is still the default and only supported version? EDIT: I guess you meant 3.x with x < 3. |
comment:43
Yes, Python 3 versions below 3.3 (and really 3.6, but I'm thinking about what Python 3 versions are still in some distros...) |
comment:44
I don't think that there is anything wrong with using And it only works on Python 3, so it would only complicate the code. If you insist, I will use it though. |
comment:45
No, it's okay. I just think it goes without saying that internal functions should be avoided. This whole thing could be written in pure Python and it seems like an overwrought pre-mature optimization not to. OTOH I feel like it's a fault on the Python side that a) this function doesn't just come built-in--sometimes it is useful, especially for introspection, to bypass some of the high-level attribute machinery, and b) that this can't currently be easily implemented by third-party C code without reliance on internal functions. Which in turn gives more credence to the idea that this should be a built-in. I think I would like to propose that to python-ideas if you don't mind, and use your code as an example. |
comment:46
Replying to @embray:
Yes, it could be written in Python. It could also be written in C. You seem to find it a problem that it's implemented in C but I don't understand why. I would argue that the C implementation is conceptually simpler than the potential Python implementation because Python doesn't have the equivalent of |
comment:47
Replying to @embray:
Feel free. But do remove the Python 2 stuff if you quote my code. |
comment:48
Python is practically pseudocode IMO. It's almost always conceptually simpler than C (especially if you're also relying on undocumented internals to do something). |
Reviewer: Erik Bray |
Changed branch from u/jdemeyer/parentmethods__use_a_metaclass_which_does_not_bind_methods to |
There are various instances in Sage of code like
This is because we typically want to get the actual function from the
ParentMethods
body without binding toParentMethods
. This may apply to more general descriptors besides functions.To solve this, I propose a new function
rawattr()
which is likegetattr()
but without binding.CC: @nthiery
Component: categories
Author: Jeroen Demeyer
Branch/Commit:
854eb48
Reviewer: Erik Bray
Issue created by migration from https://trac.sagemath.org/ticket/25179
The text was updated successfully, but these errors were encountered: