Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python __init__中的super()似乎没有实现父对象的默认关键字参数_Python - Fatal编程技术网

Python __init__中的super()似乎没有实现父对象的默认关键字参数

Python __init__中的super()似乎没有实现父对象的默认关键字参数,python,Python,我正在尝试实现一种表示(周期性)时间间隔的数据类型。我已经定义了一个Interval对象,并且正在尝试使用附加属性PeriodicInterval从该对象继承一个PeriodicInterval: from datetime import date, timedelta, datetime class Interval(object): """ An interval represents a duration of time and its location on the

我正在尝试实现一种表示(周期性)时间间隔的数据类型。我已经定义了一个
Interval
对象,并且正在尝试使用附加属性
PeriodicInterval
从该对象继承一个
PeriodicInterval

from datetime import date, timedelta, datetime

class Interval(object):
    """
    An interval represents a duration of time and its location on the
    timeline. It can be any of the following:

    - start and end dates (or datetimes)
    - a start date (or datetime) and a timedelta
    - a timedelta and an end date (or datetime)

    Provides the following operators:
        for a date and an Interval:
            in
    """

    def __init__(self, start=None, duration=None, end=None):
        # Type checking:
        assert isinstance(start, date) or (start is None)
        assert isinstance(duration, timedelta) or (duration is None)
        assert isinstance(end, date) or (end is None)

        # Fill in the missing value:
        if (duration is not None) and (end is not None) and (start is None):
            start = end - duration
        elif (start is not None) and (end is not None) and (duration is None):
            duration = end - start
        elif (start is not None) and (duration is not None) and (end is None):
            end = start + duration

        # Assign the values:
        self.start = start
        self.duration = duration
        self.end = end

    def __contains__(self, time):
        """
        Checks if a date is contained within the Interval, e.g.:

        >>> datetime.now() in Interval(datetime.now(), timedelta(1))
        True
        """
        assert isinstance(time, date), "The argument 'time' should be a date."
        return (self.start <= time) and (time <= self.end)


class PeriodicInterval(Interval):
    def __init__(self, period=None, **kwargs):
        super(PeriodicInterval, self).__init__(kwargs)


if __name__ == "__main__":
    periodic_interval = PeriodicInterval()

我不明白为什么以这种方式实例化
PeriodicInterval
会导致错误。如果使用
Interval=Interval()
实例化
Interval
,则不会得到错误。是什么导致了这种情况?

调用父构造函数时,需要使用(**)运算符分解字典关键字参数:

class PeriodicInterval(Interval):
    def __init__(self, period=None, **kwargs):
        super(PeriodicInterval, self).__init__(**kwargs)

调用父构造函数时,需要使用(**)运算符分解字典关键字参数:

class PeriodicInterval(Interval):
    def __init__(self, period=None, **kwargs):
        super(PeriodicInterval, self).__init__(**kwargs)

我懂了。在
PeriodicInterval
\uuuuu init\uuuuuu
函数中,
kwargs
是一个字典,它需要用
**
解包,以便将关键字参数传递给它的
super
。是的。如果只传递
kwargs
而不传递
**kwargs
,则整个dict将作为
Interval的
start
参数传递。因此,第一个断言失败。我明白了。在
PeriodicInterval
\uuuuu init\uuuuuu
函数中,
kwargs
是一个字典,它需要用
**
解包,以便将关键字参数传递给它的
super
。是的。如果只传递
kwargs
而不传递
**kwargs
,则整个dict将作为
Interval的
start
参数传递。因此,第一次断言失败。