8 months, 3 weeks

Positional-only parameters




There are two ways in which function arguments can be provided to functions:
• As a positional argument
• As a keyword argument

def concatenate(first: str, second: str, delim: str):             
 return delim.join([first, second])                                       

def concatenate(first: str, second: str, /, *, delim: str):      
 return delim.join([first, second])                                       
The way in which you read this definition is as follows:
• All arguments preceding the / mark are positional-only arguments
• All arguments following the * mark are keyword-only arguments

As there is only one way in which this function can be used, we can now use argument 
unpacking to implement the following change:
def concatenate(*items, delim: str):                                   
 return delim.join(items)                                                     
The *items argument will capture all the positional arguments in the items tuple. 

Positional-only and keyword-only arguments are a great tool for library creators.You can utilize positional-only and keyword-only arguments to 
make sure that functions will be invoked as intended. 


Responses(0)







Related