LoginSignup
1
0

More than 5 years have passed since last update.

EP19 Provide Optional Behavior with Keyword Arguments

Posted at
  • Function argument can be secified by position or by keywrod.
  • Keywords make it clear what the purpose of each argument is when it would be confusing with only positional arguments
  • Keyword arguments with default value make it easy to add new behaviors to a function, especially when the function has exsisting callers.
  • Optional keyword arguments should always be passed by keyword instead of by position.

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.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0