Python 使用kwargs控制方法内部的流

Python 使用kwargs控制方法内部的流,python,methods,keyword-argument,Python,Methods,Keyword Argument,我试图在一个类中编写一个方法,在这个类中,相同的方法可以用于参数根据年/月/日变化的端点。年是必填参数,月/日是可选参数。是否有一种机制来跟踪传递的参数数量并控制方法内部的流 # Global var URL = 'http://posts/archive/' # trying to use *kwargs for mn and day. year is required/positional. get_records_arch(year, mn, dy): if year only i

我试图在一个类中编写一个方法,在这个类中,相同的方法可以用于参数根据年/月/日变化的端点。年是必填参数,月/日是可选参数。是否有一种机制来跟踪传递的参数数量并控制方法内部的流

 # Global var
URL = 'http://posts/archive/'


# trying to use *kwargs for mn and day. year is required/positional.
get_records_arch(year, mn, dy):

if year only is passed:
    API_ENDPOINT = 'http://posts/archive/{year}/'.format(year=YEAR)
else if 'year and mn' are passed:
    API_ENDPOINT = 'http://posts/archive/{year}/{month}/'.format(year=YEAR, month=MON)
else
    API_ENDPOINT = 'http://posts/archive/{year}/{month}/{day}/'.format(year=YEAR, month=MON, day=DAY)
将mn和dy的默认值设为None,然后在尝试使用它们之前检查它们是否都有非None值。请注意,dy完全被忽略,除非mn不是None

def get_records_archyear,mn=None,dy=None:那么测试None似乎是最简单的。
def get_records_arch(year, mn=None, dy=None):
    API_ENDPOINT = 'http://posts/archive/{}/'.format(year)
    if mn is not None:
        API_ENDPOINT += '{}/'.format(mn)
        if dy is not None:
            API_ENDPOINT += '{}/'.format(dy)

    ...