Book Image

The Python Apprentice

By : Robert Smallshire, Austin Bingham
Book Image

The Python Apprentice

By: Robert Smallshire, Austin Bingham

Overview of this book

Experienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it’s not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.
Table of Contents (21 chapters)
Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
Preface
12
Afterword – Just the Beginning

Function arguments in detail


Now that we understand the distinction between object references and objects, we'll look at some more capabilities of function arguments.

Default parameter values

The formal function arguments specified when a function is defined with the def keyword are a comma-separated list of the argument names. These arguments can be made optional by providing default values. Consider a function which prints a simple banner to the console:

>>> def banner(message, border='-'):
...     line = border * len(message)
...     print(line)
...     print(message)
...     print(line)
...

This function takes two arguments, and we provide a default value — in this case '-' — in a literal string. When we define functions using default arguments, the parameters with default arguments must come after those without defaults, otherwise we will get a SyntaxError.

On line 2 of the function we multiply our border string by the length of the message string. This line shows two interesting...