简体   繁体   English

python3类__init__继承而无需再次定义参数

[英]python3 class __init__ inheritance without defining parameters again

Is it possible to inherit a class and use its init function without declaring all parameters again in the child class? 是否可以继承一个类并使用其init函数,而无需在子类中再次声明所有参数?

I have a class with lots of parameters, but I don't want to use a list (**args). 我有一个带有很多参数的类,但是我不想使用列表(** args)。 I wouldn't see my actual parameters: 我看不到我的实际参数:

class Table(object):
    def __init_(self, name, height ...):
        self.name = name
        self.height = height

class RoundTable(Table):
    def __init__(self, radius):
        self.radius = radius

table = RoundTable(name = "Placeholder",
                   height = 10,
                   radius = 20)

Use the super class before assigning specfic args 在分配特定的arg之前使用超类

def __init__(self,radius,*args,**kwargs):
     super().__init__(*args,**kwargs)
     self.radius = radius

Edit : I'm assuming you mean class and not a function 编辑:我假设你的意思是类而不是一个函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM