[英]python 3 error RuntimeError: super(): no arguments
為什么我會收到此錯誤? 有人可以為我解決這個問題嗎? 我試圖從Progress.display()
的類項目中調用顯示函數,或者任何人有其他關於如何顯示用戶輸入的解決方案?
以及如何同時輸入 Stages 類和 Progress 類? 感謝您的幫助
超級()。顯示()
RuntimeError: super(): 沒有參數
這是代碼
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()
不是 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)
每次在方法中使用 super() 時,都需要在實例方法或類方法中。 您的staticmethod
不知道它們的超類是什么。 觀察:
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()
不是這個問題的真正答案,但我在 pdb shell 中嘗試調用super
時遇到了同樣的錯誤,最終陷入了一個兔子洞試圖弄清楚。 您需要將要調用 super on 和 self 的父類添加到調用中,以便它在 pdb 中運行 - super(<ParentClass>, self)
。 或者至少知道super
在 pdb 中不會按預期工作。 我真的不需要在那里調用它,但它阻止了我弄清楚為什么其他東西不起作用。
您可能根本不必使用super()
,只需直接引用超類即可。 例如,我正在編寫一個像這樣的 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.