【问题标题】:python 3 error RuntimeError: super(): no argumentspython 3错误RuntimeError:super():没有参数
【发布时间】:2018-11-27 21:53:25
【问题描述】:

为什么会出现此错误?有人可以为我解决这个问题吗?我试图从Progress.display() 的类项目中调用显示函数,或者任何人有其他关于如何显示用户输入的解决方案?

如何同时输入 Stages 类和 Progress 类?感谢您的帮助p

super().display()
RuntimeError: super(): no arguments

这是代码

class Project:
    def __init__(self, name="", job="", **kwargs):
        super().__init__(**kwargs)
        self.name = name
        self.job = job

    def display():
        print("name: ", (self.name))
        print("job: ", (self.job))

    @staticmethod
    def prompt_init():
        return dict(name=input("name: "), job=input("job: "))


class Stages(Project):
    def __init__(self, stages="", **kwargs):
        super().__init__(**kwargs)
        self.stages = stages

    def display(self):
        super().display()
        print("stages: ", (self.stages))

    @staticmethod
    def prompt_init():
        parent_init = Project.prompt_init()

        choice = None
        while choice not in (1, 2, 3, 4, 5, 6):

            print("Insert your stage now: ")
            print("1. Planning")
            print("2. Analysis")
            print("3. Design")
            print("4. Implementation")
            print("5. Testing")
            print("6. Release")

            choice = input("enter your choice: ")
            choice = int(choice)

            if choice == 1:
                stages = "Planning"
            elif choice == 2:
                stages = "Analysis"
            elif choice == 3:
                stages = "Design"
            elif choice == 4:
                stages = "Implementation"
            elif choice == 5:
                stages = "Testing"
            elif choice == 6:
                stages = "Release"
            else:
                print("no such input, please try again")

            print(name)
            print(stages)


class Progress(Project):
    def __init__(self, progress="", **kwargs):
        super().__init__(**kwargs)
        self.progress = progress

    def display(self):
        super().display()
        print("progress: ", (self.progress))

    @staticmethod
    def prompt_init():
        parent_init = Project.prompt_init()

        choice = None
        while choice not in (1, 2, 3, 4):

            print("1. 25%")
            print("2. 50%")
            print("3. 75%")
            print("4. 100%")

            choice = input("enter your choice[1-4]: ")
            choice = int(choice)

            if choice == 1:
                progress = "25%"
            elif choice == 2:
                progress = "50%"
            elif choice == 3:
                progress = "75%"
            elif choice == 4:
                progress = "100%"
            else:
                print("no such input, please try again")

            print(progress)
        parent_init.update({"progress": progress})
        return parent_init


class A(Stages, Progress):
    def prompt_init():
        init = Stages.prompt_init()
        init.update(Progress.prompt_init())
        return init

    prompt_init = staticmethod(prompt_init)


class New:
    type_map = {("stages", "progress"): A}

    def add_project_test(self, name, job, stages):
        init_args = Project.prompt_init()
        self.project_list.append(Project(**init_args))

    def __init__(self):
        self.project_list = []

    def display_project():
        for project in self.project_list:
            project.display()
            print()

    def add_progress(self):
        init_args = Progress.prompt_init()
        self.project_list.append(Progress(**init_args))

    def add_project(self):
        ProjectClass = self.type_map[A]
        init_args = ProjectClass.prompt_init()
        self.property_list.append(ProjectClass(**init_args))


my_list = New()
my_list.add_progress()
my_list.display_project()

【问题讨论】:

  • 对于发现此问题和标题的其他任何人,但这不是问题:您是否尝试在类本身而不是def __init__(self): 方法中调用super().__init__()?引发了同样的错误。

标签: python python-3.x


【解决方案1】:

不是 100% 的答案,而是同样的错误。怀着对与我有同样问题的 Google 员工的热爱发布。

使用 Python 3,我收到此错误,因为我忘记在方法中包含 self。简单的事情,但有时最简单的事情会在您疲倦时绊倒您。

class foo(object):
    def bar(*args):
        super().bar(*args)

=> RuntimeError: super(): no arguments

记得添加你的self

class foo(object):
    def bar(self, *args):
        super().bar(*args)

【讨论】:

    【解决方案2】:

    不是这个问题的真正答案,但我在 pdb shell 中尝试调用 super 时遇到了同样的错误,最终陷入了一个兔子洞试图找出答案。您需要将要调用 super on 和 self 的父类添加到调用中,以便它在 pdb 中运行 - super(<ParentClass>, self)。或者至少只知道super 在 pdb 中不会按预期工作。我真的需要在那里调用它,但它阻止了我弄清楚为什么其他东西不起作用。

    【讨论】:

    • 对于那些使用 PyCharm(在后端运行 pdb)的人也是如此
    • VS code 的用户也是如此。
    【解决方案3】:

    每次在方法中使用super(),都需要在实例方法或类方法中。你的staticmethods 不知道他们的超类是什么。观察:

    class Funky:
        def groove(self):
            print("Smooth")
    
        @staticmethod
        def fail():
            print("Ouch!")
    
        @classmethod
        def wail(cls):
            print("Whee!")
    
    
    class Donkey(Funky):
        def groove(self):
            print(super())
    
        @staticmethod
        def fail():
            try:
                print(super())
            except RuntimeError as e:
                print("Oh no! There was a problem with super!")
                print(e)
    
        @classmethod
        def wail(cls):
            print(super())
    
    
    a_donkey = Donkey()
    a_donkey.groove()
    a_donkey.fail()
    a_donkey.wail()
    

    输出:

    <super: <class 'Donkey'>, <Donkey object>>
    Oh no! There was a problem with super!
    super(): no arguments
    <super: <class 'Donkey'>, <Donkey object>>
    

    这是您的代码,经过调试并带有一些额外的功能和测试:

    class Project:
        def __init__(self, name="", job="", **kwargs):
            super().__init__(**kwargs)
            self.name = name
            self.job = job
    
        def display(self):
            print("name: ", self.name)
            print("job: ", self.job)
    
        @staticmethod
        def prompt_init():
            return dict(name=input("name: "), job=input("job: "))
    
    
    class Progress(Project):
        def __init__(self, progress="", **kwargs):
            super().__init__(**kwargs)
            self.progress = progress
    
        def display(self):
            super().display()
            print("progress: ", self.progress)
    
        @staticmethod
        def prompt_init():
            parent_init = Project.prompt_init()
            progress = input("your progress: ")
            parent_init.update({
                "progress": progress
            })
            return parent_init
    
    
    class New:
        def __init__(self):
            self.project_list = []
    
        def display_project(self):
            for project in self.project_list:
                project.display()
                print()
    
        def add_project(self):
            init_args = Project.prompt_init()
            self.project_list.append(Project(**init_args))
    
        def add_progress(self):
            init_args = Progress.prompt_init()
            self.project_list.append(Progress(**init_args))
    
    
    my_list = New()
    my_list.add_project()
    my_list.add_progress()
    my_list.display_project()
    

    【讨论】:

    • 感谢帮助的朋友!真的很感激,我可以再问你一件事吗?现在我的代码中除了 Progress 之外还有 Stages 类。我怎样才能同时输入它们?有什么想法吗?
    • @JustinJunias 你确定 Stages 是一个类吗?或者它可能是Progress 类的一个属性?就像,也许Progress 可以有一个像现有的self.progress 这样的变量,你可以创建一个名为set_stage(a_stage)(或advance_stage())的新方法来更新self.progress?您可能还更改了progressprompt_init 以不接受progress 变量的用户输入,而是始终将起始progress 设置为“已添加到系统”或其他内容。只是一个想法:-)
    【解决方案4】:

    您可能根本不必使用super(),只需直接引用超类即可。例如,我正在编写一个像this one 这样的Django 测试,但在我的例子中,AnimalTestCase 继承了ParentTestCase。我希望 AnimalTestCase 中的 fixture 属性使用 ParentTestCase 中的所有相同装置,并添加更多。但是打电话给super() 从来没有奏效。最后,我意识到我可以照原样引用ParentTestCase

    fixtures = ParentTestCase.fixtures + ['more']

    class ParentTestCase(TestCase):
        fixtures = ['bacteria', 'fungus', 'stalagtites', 'stalagmites']
    
        def setUp(self):
            # Test definitions as before.
            call_setup_methods()
    
    
    class AnimalTestCase(ParentTestCase):
        fixtures = ParentTestCase.fixtures + ['vertebrata', 'invertebrate']
    
        def test_fluffy_animals(self):
            # A test that uses the fixtures.
            call_some_test_code()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      相关资源
      最近更新 更多