Changing calling sequence

Tobiah toby at tobiah.org
Wed May 11 11:36:26 EDT 2022


On 5/11/22 06:33, Michael F. Stemper wrote:
> 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)



More information about the Python-list mailing list