为什么使用super().__ init__我只能在父级中初始化第一个参数

发布于 2025-02-11 21:25:48 字数 1253 浏览 4 评论 0原文

不明白为什么这有效:

class Fish:
    def __init__(self, first_name, last_name="Fish"):
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, water = "freshwater"):
        self.water = water
        super().__init__(self)

terry = Trout()

# Initialize first name
terry.first_name = "Terry"

# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)

# Use child __init__() override
print(terry.water)

但是,如果我希望每个孩子都有不同的last_name y无法删除其值,并像以后一样定义它,就像我们使用first_name一样:

class Fish:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, water = "freshwater"):
        self.water = water
        super().__init__(self)

terry = Trout()

# Initialize first name
terry.first_name = "Terry"
terry.last_name =  "Crews"

# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)

# Use child __init__() override
print(terry.water)
Error: TypeError: Fish.__init__() missing 1 required positional argument: 'last_name'

Don't understand why this works:

class Fish:
    def __init__(self, first_name, last_name="Fish"):
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, water = "freshwater"):
        self.water = water
        super().__init__(self)

terry = Trout()

# Initialize first name
terry.first_name = "Terry"

# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)

# Use child __init__() override
print(terry.water)

But if I would like every child class to have different last_name y can't delete its value and define it later like we did with first_name:

class Fish:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, water = "freshwater"):
        self.water = water
        super().__init__(self)

terry = Trout()

# Initialize first name
terry.first_name = "Terry"
terry.last_name =  "Crews"

# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)

# Use child __init__() override
print(terry.water)
Error: TypeError: Fish.__init__() missing 1 required positional argument: 'last_name'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏闹i 2025-02-18 21:25:48

如果要在构造函数中设置属性,则需要实际将它们作为参数传递;您的孩子班需要接受它们作为参数,以便它可以将它们传递给父班。

class Fish:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, first_name, last_name, water = "freshwater"):
        self.water = water
        super().__init__(first_name, last_name)

terry = Trout("Terry", "Crews")

print(terry.first_name + " " + terry.last_name)
print(terry.water)

如果您不打算使用构造函数来设置属性,请删除这些参数,然后将属性设置为默认值(例如none):

class Fish:
    def __init__(self):
        self.first_name = None
        self.last_name = None

class Trout(Fish):
    def __init__(self, water = "freshwater"):
        self.water = water
        super().__init__()

terry = Trout()
terry.first_name = "Terry"
terry.last_name = "Crews"

print(terry.first_name + " " + terry.last_name)
print(terry.water)

您还可以在父类中提供被覆盖的默认值默认情况下,儿童班级可以被选可选论点覆盖,例如:

class Fish:
    def __init__(self, water, first_name, last_name="Fish"):
        self.water = water
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, first_name, last_name="Trout"):
        super().__init__("freshwater", first_name, last_name)

terry = Trout("Terry", "Crews")

print(terry.first_name + " " + terry.last_name)
print(terry.water)

If you want to set the attributes in the constructor, you need to actually pass them as arguments; your child class needs to accept them as arguments so that it can pass them to the parent class.

class Fish:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, first_name, last_name, water = "freshwater"):
        self.water = water
        super().__init__(first_name, last_name)

terry = Trout("Terry", "Crews")

print(terry.first_name + " " + terry.last_name)
print(terry.water)

If you don't plan to use the constructor to set the attributes, remove those parameters and just set the attributes to default values (e.g. None):

class Fish:
    def __init__(self):
        self.first_name = None
        self.last_name = None

class Trout(Fish):
    def __init__(self, water = "freshwater"):
        self.water = water
        super().__init__()

terry = Trout()
terry.first_name = "Terry"
terry.last_name = "Crews"

print(terry.first_name + " " + terry.last_name)
print(terry.water)

You can also provide defaults in the parent class that are overridden by defaults in the child class that can in turn by overridden by optional arguments, e.g.:

class Fish:
    def __init__(self, water, first_name, last_name="Fish"):
        self.water = water
        self.first_name = first_name
        self.last_name = last_name

class Trout(Fish):
    def __init__(self, first_name, last_name="Trout"):
        super().__init__("freshwater", first_name, last_name)

terry = Trout("Terry", "Crews")

print(terry.first_name + " " + terry.last_name)
print(terry.water)
两相知 2025-02-18 21:25:48

使用super().__ INT __()方法时,您需要精确使用所继承的参数。即使在鳟鱼构造器内部,您也需要提及其所有参数,包括其继承的IE def __init __(self,first_name,last_name,water =“ freshwater”)。因此,您的鳟鱼课应该看起来像下面的代码

class Trout(Fish):
  def __init__(self, first_name, last_name, water="freshwater"):
    self.water=water
    super().__init__(first_name, last_name)

When using super().__init__() method, you need to precise the parameters you are inheriting. and even inside your Trout constructor you need to mention all its parameters including the ones its inheriting i.e def __init__(self, first_name, last_name, water="freshwater"). So your Trout class should look like the code below

class Trout(Fish):
  def __init__(self, first_name, last_name, water="freshwater"):
    self.water=water
    super().__init__(first_name, last_name)
梦幻的心爱 2025-02-18 21:25:48

由于子类没有必要的信息,因此您没有初始化父级的任何论点。另外,您无需在打电话时提供自我。

You are not initialising any arguments of the parent class since child class doesn't have the necessary information. Also you don't need to provide self when calling super.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文