Skip to Content
Intuitive Python
book

Intuitive Python

by David Muller
May 2021
Intermediate to advanced
142 pages
3h 37m
English
Pragmatic Bookshelf
Content preview from Intuitive Python

Getting Caught with Sticky Default Arguments

Python allows you to specify arguments with defaults in your function signatures. For example, consider the use_exclamation argument in the print_greeting function:

 def​ ​print_greeting​(name, use_exclamation=False):
  greeting = ​"Hello "​ + name
 if​ use_exclamation:
  greeting += ​"!"
 print​(greeting)

In the above example, the use_exclamation argument to the print_greeting function has a default value of False. Since use_exclamation defaults to False, print_greeting("David") outputs Hello David (with no trailing ! character). If we provided a value for the use_exclamation argument, however, the output changes. print_greeting("David", use_exclamation=True) outputs Hello David! because the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pythonic Programming

Pythonic Programming

Dmitry Zinoviev
Python for Geeks

Python for Geeks

Muhammad Asif

Publisher Resources

ISBN: 9781680508635Errata Page