EP19 Provide Optional Behavior with Keyword Arguments

  • Function argument can be secified by position or by keywrod.

Effective Python

def remidner(number, divisor):
    return number % divisor

It can be called by


reminder(20,7)
reminder(20, 7)
reminder(number=20, divisor=7)
reminder(divisor=7, number=20)

This expression is confusing.

twitter_search('@obama', False, 20, True)

twitter_search('@obama', retweets=False, numtweets=20, popular=True)

Is slightly (microseconds) slower but is worth it for the code clarity and developer time savings.

Recommended Posts

EP19 Provide Optional Behavior with Keyword Arguments
EP19 Provide Optional Behavior with Keyword Arguments
Effective Python Memo Item 19 Give optional behavior to keyword arguments
Positional arguments, keyword arguments, default arguments
Dictionary of keyword arguments