Check out a free preview of the full Python Fundamentals course

The "Empty Default Lists" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina warns the audience against using empty default lists as a default parameter, due to the mutability rules that will be discussed later in the course.

Preview
Close

Transcript from the "Empty Default Lists" Lesson

[00:00:00]
>> Nina Zakharenko: Well, we'll talk a little bit more about this later but I wanna introduce it as I introduced the concept. You never ever want to use mutable types like list as your default arguments? And it's okay if you don't know what that means right now. I'm just gonna show you that if you use an argument that can be changed, like a list, as your default argument, you're gonna have a very bad time.

[00:00:29]
Because you would think that these default arguments get kind of created or instantiated every time you call the function, but that's not true, actually. They only get created every time the function is called, or I'm sorry, they get created the first time the function is called and then that value is shared.

[00:00:52]
So I've seen this by so many beginner Python programmers. So like warning, don't do this. And we'll talk about lists and the methods that we're gonna use a little bit later. So let's say I had a function foo and it took one default parameter a, and a is a list, oops, sorry.

[00:01:20]
I'm gonna make it take t parameters, a is an item, just a regular positional argument, b is a list, and then I'm going to append the value of a to b, okay? So if I call this foo with five, sorry, I forgot something. My function doesn't do anything, why is that?

[00:01:50]
>> Speaker 2: There's no return.
>> Nina Zakharenko: There's no return, right. So I can do a return or I can just print out something, so I'm gonna do a print for now. Okay, if I call foo with five we'll see that b is five. If I call foo again with six, we'll see that our list b is now five and six.

[00:02:17]
So it's just using the same list over and over not doing what we expect it to. So if we want to kind of use an empty list or just use the centino value instead. You can say b equals none, right? And then in your code check and then say if b is none then make a new list within my function code.

[00:02:41]
Everyone got that? I've been bitten by this bug so many times even after I've been doing Python for years, okay. So, naming our functions and arguments, because Python is a dynamic language, we have to be really mindful of how we name things. Sometimes it's also called duck typed, which I think is a pretty funny name.

[00:03:09]
>> Nina Zakharenko: As I'm typing here in the ripple, I'm trying to show you concepts quickly. It's okay, if I call things foo and a and b. Generally, you don't wanna do this in your production code. You wanna name your functions and your variables, in what's being done with them as explicitly as possible.

[00:03:30]
For example, maybe x and y are common names in a mathematical operation, but besides for that unless they're gonna be easily recognized, you don't really want to name your things x, y, a, b [LAUGH]. Because for example, who has looked at some production code and they're like, my God, who wrote this?

[00:03:53]
This is horrible and then they did a get blame and it's like, I wrote it a year ago.
>> Speaker 2: [LAUGH]
>> Nina Zakharenko: Okay, yeah, some laughs, yeah. I definitely have done that, right?
>> Nina Zakharenko: You tend to forget, so by being as explicit as possible the first time around, not only are you being kind to your future self, you're also being kind to your coworkers.

[00:04:23]
I put a link into a talk that I really like, a conference talk that you can watch after the course called the naming of Darkseid dynamic type smart conventions by Brandon Rhodes. And he dive into this half an hour or 45 minutes just talking about how do we do a good job of naming things in a dynamic language.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now