This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2017年08月04日 06:42 by ashwch, last changed 2022年04月11日 14:58 by admin. This issue is now closed.
| Messages (4) | |||
|---|---|---|---|
| msg299733 - (view) | Author: Ashwini Chaudhary (ashwch) * | Date: 2017年08月04日 06:42 | |
From this question I noticed that super() doesn't work with static methods with no arguments: https://stackoverflow.com/q/45498675/846892 My question is what's the issue with using __class__ for both arguments to super() in case of static method? Or was it just an oversight. |
|||
| msg300594 - (view) | Author: Raymond Hettinger (rhettinger) * (Python committer) | Date: 2017年08月20日 03:30 | |
Do you have a use case? This seems doesn't seem like it would be helpful at all and would make super a little more confusing than it already is. |
|||
| msg300597 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2017年08月20日 05:14 | |
Thanks for the suggestion, but I'm closing this RFE, as there are two strong arguments against it: one related to implementation feasibility, and one related to the nature of what super() *does*. The feasibility argument is that the compiler doesn't even have the necessary information to do this, since it doesn't know what kind of method it's dealing with (it has no idea what decorators actually do, it just compiles them as subexpressions). Instead, the compiler blindly translates "super()" into "super(__class__, <first positional argument>)" when inside a function definition that's inside a class definition. As a result, there's no way for the compiler to special case static methods and pass __class__ as the second argument as well. Even without considering that technical limitation though, using __class__ for both arguments doesn't do anything particularly sensible, since super() is specifically about resolving name lookups using the *dynamic* method resolution order (MRO) of the class the method was retrieved from (which may be a subclass of the class defining the method). So in instance methods and class methods, type(self) or cls provides the dynamic MRO to use, while __class__ identifies *where* along that MRO the currently running method implementation sits. In a static method, there's no dynamic MRO available for super() to query. This means that if you want access to one, you need to convert the method to a classmethod first so that the interpreter will pass in a reference to the class the method was retrieved from (as opposed to the one where it was defined, which is what __class__ gives you). If folks actually do want to make a method implementation in a child class depend on a static method in a parent class, there are two ways to do that, but only one of them will work correctly in the face of multiple inheritance: 1. Make the child method a class method. This is the most readable approach, since you can then use zero-argument super() to call the staticmethod in the parent class in the usual way. As long as all child classes overriding the method adhere to this approach, that particular class hierarchy will also reliably respect Python's MRO linearisation rules in the case of multiple inheritance. 2. Make the child method a staticmethod, and then use "super(__class__, __class__)" to access the staticmethod in the parent class. This will work OK in cases where there aren't any diamonds in the inheritance hierarchy, but it completely bypasses the dynamic C3 linearisation algorithm, and hence may do weird things in the presence of such diamonds (i.e. cases where a subclass has multiple inheritance paths back to a given base class - see https://python-history.blogspot.com.au/2010/06/method-resolution-order.html for more detail on this). |
|||
| msg316527 - (view) | Author: Denis Ryzhkov (denisr) | Date: 2018年05月14日 16:02 | |
Simple explanation: if you call a method of a parent, then you are using class information now, so your child method is not static any more. Please use classmethod decorator and super() with no arguments - it will use cls argument implicitly |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:49 | admin | set | github: 75301 |
| 2018年05月14日 16:02:02 | denisr | set | nosy:
+ denisr messages: + msg316527 |
| 2017年08月20日 05:14:08 | ncoghlan | set | status: open -> closed resolution: rejected messages: + msg300597 stage: resolved |
| 2017年08月20日 03:30:05 | rhettinger | set | nosy:
+ rhettinger messages: + msg300594 |
| 2017年08月04日 06:47:25 | serhiy.storchaka | set | nosy:
+ ncoghlan |
| 2017年08月04日 06:44:18 | ashwch | set | components: + Interpreter Core |
| 2017年08月04日 06:42:55 | ashwch | create | |