Kurt Peek Kurt Peek - 12 days ago 6
Python Question

super() in __init__ does not seem to implement default keyword arguments of the parent object

I'm trying to implement a data type representing a (periodic) time interval following https://pypi.python.org/pypi/datetime-interval/0.2. I've defined an

Interval
object, and am trying to make a
PeriodicInterval
object inherit from it with the additional attribute
period
:

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()


However, this leads to the following
AssertionError
in the type checking:

Traceback (most recent call last):
File "/home/kurt/dev/scratch/Furion_scheduler/interval.py", line 67, in <module>
periodic_interval = PeriodicInterval()
File "/home/kurt/dev/scratch/Furion_scheduler/interval.py", line 50, in __init__
super(PeriodicInterval, self).__init__(kwargs)
File "/home/kurt/dev/scratch/Furion_scheduler/interval.py", line 20, in __init__
assert isinstance(start, date) or (start is None)
AssertionError


I don't understand why instantiating a
PeriodicInterval
in this way leads to an error. If I instantiate an
Interval
using
interval = Interval()
, I don't get an error. What is causing this?

Answer

You need to need to explode dictionary keyword arguments using the double star (**) operator when calling parent constructor:

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