英文:
Beginner error: TypeError: Molecularbiology.__init__() takes 5 positional arguments but 6 were given
问题
我正在学习Python,处于基础水平。我试图创建新的子类,并覆盖__init__
方法,但在继承父类时出现了位置错误。你能帮助我理解这个问题吗?我尝试修复缩进并进行一些尝试。
我收到以下错误信息:
TypeError: Molecularbiology.__init__()接受5个位置参数,但提供了6个
class Employee:
raise_amount = 1.08
def __init__(self, first, last, pay, prog_lang):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@gmail.com'
class Developer(Employee):
def __init__(self, first, last, pay, prog_lang): # 继承
super().__init__(first, last, pay)
self.prog_lang = prog_lang
class Molecularbiology(Employee):
def __init__(self, first, last, pay, mol_bio): # 继承
super().__init__(first, last, pay)
self.mol_bio = mol_bio
dev_1 = Molecularbiology('Kubri', 'Lyreco', 100000, 'RNASeq')
dev_2 = Molecularbiology('Vikki', 'Suomalainen', 50000, 'Microscopy')
print(dev_1.email)
print(dev_1.mol_bio)
我想在继承父类的参数的同时包括子类。
英文:
I'm learning Python and at the basic level. I'm trying to create new subclasses with __init__
override but it gives me positional errors when inheriting from the parent class. Can you please help me in understanding this. I tried fixing my indentation and playing around.
I get the following error:
TypeError: Molecularbiology.__init__() takes 5 positional arguments but 6 were given
class Employee:
raise_amount = 1.08
def __init__(self, first, last, pay, prog_lang):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@gmail.com'
class Developer(Employee):
def __init__(self, first, last, pay, prog_lang): #inheritance
super().__init__(first, last, pay)
self.prog_lang = prog_lang
class Molecularbiology(Employee):
def __init__(self, first, last, pay, mol_bio): #inheritance
super().__init__(first, last, pay, prog_lang)
self.mol_bio = mol_bio
dev_1 = Molecularbiology(' Kubri', 'Lyreco', 100000, 'Python', 'RNASeq')
dev_2 = Molecularbiology('Vikki', 'Suomalainen', 50000, 'Javascript', 'Microscopy')
print(dev_1.email)
print(dev_1.prog_lang)
print(dev_1.mol_bio)
I want to include the subclass while inheriting arguments from the parent class
答案1
得分: 1
变量的命名使人难以理解你想做什么,因此难以给你一个更一般的答案。
回答你的确切问题,Molecularbiology()
需要 4 个显式参数:在实例化类时不应显式传递 self
参数,如果不考虑 self
,__init__
方法有 4 个参数。你需要删除一个实例化参数(不太清楚是哪一个)来修复这个问题。
然后你将会遇到另一个错误,因为这一行 super().__init__(first, last, pay, prog_lang)
意味着 prog_lang
变量在作用域内可用(即它是封装的 __init__
方法的参数),但实际上它并不是。修复此错误取决于你确切想要做什么。
英文:
The naming of the variables makes hard to understand what you want to do and thus to give you a more general answer.
To answer your exact question, Molecularbiology()
requires 4 explicit arguments: the self
argument should not be explicitly passed when you instantiate the class, and the __init__
method has 4 arguments if you exclude the self
. You need to remove one of the instantiation argument (not very clear which one) to fix this.
You will have another error then, because the line super().__init__(first, last, pay, prog_lang)
implies that the prog_lang
variable is available in the scope (ie it is an argument of encompassing __init__
method), and it is not. Fixing this error depends on what you want to do exactly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论