Python function that takes one compulsory argument from two choices

后端 未结 3 1351
Happy的楠姐
Happy的楠姐 2021-01-18 16:38

I have a function:

def delete(title=None, pageid=None):
    # Deletes the page given, either by its title or ID
    ...
    pass

However, m

3条回答
  •  一整个雨季
    2021-01-18 16:59

    There is no Python syntax that'll let you define exclusive-or arguments, no. You'd have to explicitly raise an exception is both or none are specified:

    if (title and pageid) or not (title or pageid):
        raise ValueError('Can only delete by title OR pageid')
    

提交回复
热议问题