I took a look at this question but it doesn't exactly answer my question. As an example, I've taken a simple method to print my name.
def call_me_by_name(first_name):
print("Your name is {}".format(first_name))
Later on, I realized that optionally, I would also like to be able to print the middle name and last name. I made the following changes to accommodate that using **kwargs fearing that in the future, I might be made to add more fields for the name itself (such as a 3rd, 4th, 5th name etc.)
I decided to use **kwargs
def call_me_by_name(first_name,**kwargs):
middle_name = kwargs['middle_name'] if kwargs.get('middle_name') else ""
last_name = kwargs['last_name'] if kwargs.get('last_name') else ""
print("Your name is {} {} {}".format(first_name,middle_name,last_name))
My only concern here is that as I continue to implement support for more names, I end up writing one line of code for every single keyword argument that may or may not come my way. I'd like to find a solution that is as pythonic as possible. Is there a better way to achieve this ?
EDIT 1
I want to use keyword arguments since this is just an example program. The actual use case is to parse through a file. The keyword arguments as of now would support parsing a file from
1) A particular byte in the file.
2) A particular line number in the file.
Only one of these two conditions can be set at any given point in time (since it's not possible to read from a particular byte offset in the file and from a line number at the same time.) but there could be more such conditions in the future such as parse a file from the first occurrence of a character etc. There could be 10-20 different such conditions my method should support BUT only one of those conditions would ever be set at any time by the caller. I don't want to have 20-30 different IF conditions unless there's no other option.