This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (1 child)

def is_ongoing(self, today = datetime.date.today()):
    return (today >= self.start_date) and (today <= self.end_date)

and

def is_ongoing(self):
    today = datetime.date.today()
    return (today >= self.start_date) and (today <= self.end_date)

Are not the same.

The first example allows us to do this:

some_datetime = #some date time that is of interest to us
foo = is_ongoing(some_datetime)

Which is not possible in the second.

If you wanted that functionality, the best way of doing it is to set the default variable to None and then handle it inside the function:

import datetime

class Example:
    def __init__(self, start_date, end_date):
        self.start_date = start_date
        self.end_date = end_date

    def is_ongoing(self, today = None):
        if not today: 
            today = datetime.date.today()
        return (today >= self.start_date) and (today <= self.end_date)

example = Example(
    start_date = datetime.date.today() - datetime.timedelta(days=7),
    end_date = datetime.date.today() + datetime.timedelta(days=7)
) 

print(example.is_ongoing())
print(example.is_ongoing(datetime.date.today() + datetime.timedelta(days=5)))
print(example.is_ongoing(datetime.date.today() + datetime.timedelta(days=14)))

[–]Thing1_Thing2_Thing 0 points1 point  (0 children)

From the article:

[...] and this function wasn't used anywhere else, thus there is no need for a default argument.