General¶
Named Parameters¶
Use named parameters for inline primitive types (when it makes the call site easier to understand).
twitter_search("@obama", retweets=False, numtweets=20, popular=True)
twitter_search("@obama", False, 20, True)
# Good
twitter_search("@obama", retweets=False, numtweets=20, popular=True)
# Bad
twitter_search("@obama", False, 20, True)
Note
Consider using keyword only arguments when defining API’s.
💡 learnt from:
Source: Raymond Hettinger
Reference: Transform Python Slides
Unpacking¶
person = "John", "Doe", 42, "john.doe@example.net"
firstname, lastname, age, email = person
person = "John", "Doe", 42, "john.doe@example.net"
firstname = person[0]
lastname = person[1]
age = person[2]
email = person[3]
# Good
person = "John", "Doe", 42, "john.doe@example.net"
firstname, lastname, age, email = person
# Bad
person = "John", "Doe", 42, "john.doe@example.net"
firstname = person[0]
lastname = person[1]
age = person[2]
email = person[3]
💡 learnt from:
Source: Raymond Hettinger
Reference: Transform Python Slides