将构造函数参数隐式传递给基础构造函数

时间:2017-07-01 01:01:25

标签: python python-3.x peewee

我可以避免在派生类中输入所有基类构造函数参数吗?即,请参阅下面的简单代码:

class Base():
    def __init__(self, param1, param2, .... param10)

class Derived(Base):
    def __init__(self, do I have to retype out all these params again?)
        # also if the base params change I'll need to re-edit this constructor aswell
        # is there an easier way?

我的类继承自一个基类,其构造函数有多个参数(~10)。所以我要么我的类构造函数也有这些参数,或者python有什么东西我不需要实际输入它们?

继承我的用例:

class MyModel(peewee.Model):

    ID      = peewee.PrimaryKeyField()
    name    = peewee.CharField()
    ... many more fields

    def onClick(self, e):
        raise NotImplementedError("method 'MyModel::onClick' should be overridden!")

# MyModel instances can be created as so...
m = MyModel(name='bar', ...) # lots of properties

class FooModel(MyModel):

    # Do I HAVE to explicitly state each parameter? Is there an easier way?
    def __init__(self, ...):
        MyModel.__init__(self, ...)
        # initialise some properties
        self.some_property = 1
        self.another_property = 'foo'

    def onClick(self, e):
        # custom click handling
        ...

# I want to instantiate FooModel's the same way'
m = FooModel(name='bar', ...) # lots of properties

0 个答案:

没有答案