>> I have a function that I use to retrieve daily data from a
>> home-brew database. Its calling sequence is;
>> 
>> def TempsOneDay( year, month, date ):
>> 
>> After using it (and its friends) for a few years, I've come to
>> realize that there are times where it would be advantageous to
>> invoke it with a datetime.date as its single argument.
>
>You could just use all keyword args:
>
>def TempsOneDay(**kwargs):
>
>         if 'date' in kwargs:
>                 handle_datetime(kwargs['date'])
>         elif 'year' in kwargs and 'month' in kwargs and 'day' in kwargs:
>                 handle_args(kwargs['year'], kwargs['month'], kwargs['day'])
>         else:
>                 raise Exception("Bad keyword args")
>
>TempsOneDay(date=datetime.datetime.now)
>
>TempsOneDay(year=2022, month=11, day=30)
>

Maybe not the prettiest, but you could also define it like this, which also 
wouldn't require changing of any existing calls or the main body of the 
function past this if block.

def TempsOneDay(*dateComponents):
    if len(dateComponents) == 3:
        year, month, date = dateComponents
    elif len(dateComponents) == 1 and isinstance(dateComponents[0], 
datetime.date):
        year, month, date = (dateComponents[0].year, dateComponents[0].month, 
dateComponents[0].day)
    else:
        raise Exception("Error message here")
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to