简体   繁体   中英

accept multiple date arguments in a function

Bonjour! I want to write program to calculate 28 day cycle for when rent is due, but if atthe end of year i calculate the no of days i paid the rent late , i wish to total those days and calculate them based on paid dates in my code. the question is how to insert multiplepaidDates for the whole year ie 12 for calculating the days i paid late? how to insert multiple arguments for it in the function datespan?

link: How to iterate over a timespan after days, hours, weeks and months in Python?

help appreciated. Merci.

from datetime import date, datetime, timedelta

def datespan(startDate, endDate, paidDate,delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate < endDate:
        yield currentDate
        currentDate += delta
        dl = paidDate - currentDate
        print (dl)

for day in datespan(date(2015,3,12 ), date(2015,12,31), date((2015,4,15), date(2015,5,14)),
     delta=timedelta(days=28)):
     print (day)

It looks like you want to use varargs. This is possible with python. You would change your method signature to be:

def datespan(startDate, endDate, delta, *paidDates)

This will allow you to pass in as many paidDates arguments as you want. Note that you will not be able to use the default arg for delta anymore.

datespan(date(2015,3,12), date(2015,12,31), timedelta(days=28), date(2015,4,15), date(2015,5,14))

should work fine in this case. You will also need to modify your function to handle a sequence of paidDates as opposed to just one coming in as an argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM