1

I'm trying to define a class RecurringInterval which uses the rrule class from dateutil.rrule through composition, and in addition has the attribute period which by default is None. I've tried to initialize it in this way:

class RecurringInterval(object):
    def __init__(self, *args, period=None, **kwargs):
        self.period = period
        self.rrule = dateutil.rrule.rrule(*args, **kwargs)

recurring_interval = RecurringInterval(dateutil.rrule.DAILY, count=1)

However, I get a SyntaxError:

  File "/home/kurt/dev/scratch/Furion_scheduler/recurring_interval.py", line 7
    def __init__(self, *args, period=None, **kwargs):
                                   ^
SyntaxError: invalid syntax

As I understand it, positional arguments should come before keyword arguments, so this is how I would expect the syntax to be; how would I correct it? (From https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists this is not yet clear to me).

I tried bringing period=None forward, like so:

class RecurringInterval(object):
    def __init__(self, period=None, *args, **kwargs):
        self.period = period
        self.rrule = dateutil.rrule.rrule(*args, **kwargs)

but this gives rise to a TypeError:

Traceback (most recent call last):
  File "/home/kurt/dev/scratch/Furion_scheduler/recurring_interval.py", line 9, in <module>
    recurring_interval = RecurringInterval(dateutil.rrule.DAILY, count=1)
  File "/home/kurt/dev/scratch/Furion_scheduler/recurring_interval.py", line 7, in __init__
    self.rrule = dateutil.rrule.rrule(*args, **kwargs)
TypeError: __init__() takes at least 2 arguments (2 given)

How can I initialize the RecurringInterval in the intended fashion?

2
  • Are you using Python 2 or Python 3? I think you can definitely do what you're trying to do there in python 3, but might not be possible in 2. The docs you're referring to are covering function calling rather than function definition. Commented Nov 23, 2016 at 16:36
  • 1
    Hammerite, I'm using Python 2. With the __init__ as __init__(self, period=None, *args, **kwargs), I tried running the code in Python 3, but I then get TypeError: __init__() missing 1 required positional argument: 'freq'. Commented Nov 23, 2016 at 16:46

2 Answers 2

4

It should be:

def __init__(self, period=None, *args, **kwargs):
Sign up to request clarification or add additional context in comments.

2 Comments

Daniel, I tried this, but it leads to a TypeError rather than a SyntaxError (see the latter part of the question). How would I 'totally' fix the code?
@KurtPeek that's another different error than your question
1

Updated answer

Following Python, default keyword arguments after variable length positional arguments, the following works in Python 3:

class RecurringInterval(object):
    def __init__(self, *args, duration=datetime.timedelta(seconds=0), **kwargs):    # Initializing in this way only works in Python 3
        self.duration = duration
        self.rrule = dateutil.rrule.rrule(*args, **kwargs)

Old answer

Following this Github article, I found a solution using kwargs.pop:

class RecurringInterval(object):
    def __init__(self, *args, **kwargs):
        self.period = kwargs.pop('period', None)
        self.rrule = dateutil.rrule.rrule(*args, **kwargs)

recurring_interval = RecurringInterval(dateutil.rrule.DAILY, count=1, period=datetime.timedelta(days=2))

This way, period is given as a keyword argument, it is assigned to self.period, the default value of which is None, while the remaining args and kwargs are used to initialize the RecurringInterval's self.rrule.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.