Get “super(): no arguments” error in one case but not a similar case

后端 未结 1 800
感情败类
感情败类 2021-01-02 05:13
class Works(type):
    def __new__(cls, *args, **kwargs):
        print([cls,args]) # outputs [, ()]
        return super().__new__(c         


        
相关标签:
1条回答
  • 2021-01-02 06:00

    The zero-argument form of super requires that the method containing it have an explicit (i.e., non-varargs) first argument. This is suggested by an older version of the docs (emphasis added):

    The zero argument form automatically searches the stack frame for the class (__class__) and the first argument.

    For some reason this note was removed in later versions of the docs. (It might be worth raising a doc bug, because the docs are quite vague about how zero-argument super works and what is required for it to work.)

    See also this Python bug report (which is unresolved, and not clearly accepted as even a bug). The upshot is that zero-argument super is magic, and that magic fails in some cases. As suggested in the bug report, if you want to accept only varargs, you'll need to use the explicit two-argument form of super.

    0 讨论(0)
提交回复
热议问题