Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Providing Default Argument Values

00:00 Providing Default Argument Values. Using name constants to provide default argument values to functions, methods and classes is another common practice in Python.

00:11 There are lots of examples of this practice in the Python standard library. For example, the zipfile module provides tools to create, read, write, append, and list zip files.

00:22 The most relevant class in this module is ZipFile. With ZipFile, you can manipulate your zip files efficiently and quickly.

00:30 The class structure of ZipFile takes an argument called compression, which allows you to select among a few available data compression methods.

00:38 This argument is optional and has ZIP_STORED as its default value, meaning the zip file doesn’t compress the input data by default.

00:48 Here, ZIP_STORED is a constant defined in zipfile. The constant holds a numeric value for uncompressed data. You’ll also find other compression methods represented by name constants such as ZIP_DEFLATED for the deflate compression algorithm.

01:02 The compression argument in the ZipFile class constructor is a good example of using constants to provide default argument values when you have an argument that can take only a limited number of valid values.

01:14 Another example of when constants come in handy as default argument values is when you have several functions with a recurrent argument. Let’s say you are developing an application that connects to a local SQL-like database.

01:27 Your app would use a set of functions to manage the database similar to what’s seen on screen.

01:42 These functions perform different actions on your SQL-like database. Note that all the functions share the db_path argument. While you’re developing the application, you decide to provide a default database path to your functions so that you can quickly test them.

01:58 In this case, you can directly use the path as a default value to the db_path argument, but it’s better to use a name constant to provide the default database path.

02:21 This update enables you to quickly test your app by targeting a sample database during development. It also improves the maintainability of your code because you can reuse this constant in other database related functions that appear in future versions of the app.

02:37 Finally, you’ll find situations in which you want to pass an object with certain behavior to a class, method, or function. This practice is known as duck typing and is a fundamental principle in Python.

02:50 Let’s say your code will take care of providing a standard implementation of the required object. If your users want a custom object, then they should provide it themselves.

03:00 In this situation, you can use a constant to define the default object and then pass the constant as a default argument to the target class, method, or function.

03:10 In this example, this will be a FileHandler class, but first, a reader will need to be created so onscreen, here is a toy implementation of a Reader class.

03:24 The read method in this example takes the path to a file, opens it, and prints its content to the screen line by line. This class will play the role of your default reader.

03:40 The final step in the code is to create a constant DEFAULT_READER to store an instance of the DefaultReader.

03:48 With that code in place, the FileHandler class can be created.

04:03 This class provides a way to manipulate different types of files. The read method uses the injected reader object to read the input file according to its specific format.

04:14 You now have a class that processes the input files and also a helper class that provides the default reader. Your users can also code custom readers. They can code readers for CSV and JSON files, and once they’ve written a given reader, they can pass it into the FileHandler class constructor and use the resulting instance to handle files that use the readers target file format.

04:39 In the next section of the course, you’ll take a look at techniques for handling constants in a real-world project.

Become a Member to join the conversation.