Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Methods and Functions
Lecture: Default values for overloads

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The first Pythonic technique that we can use to address this
0:03 what you might call a shortcoming,
0:05 at least a different way of programming
0:07 around method overloading is with default values.
0:10 Let's have a look.
0:12 Here in PyCharm we have some simple method called display_greeting,
0:16 it's completely contrived but it'll totally work for our purposes.
0:19 You give it a name, you give it a greeting,
0:21 so the name might be Jeff, the greeting might be "Good morning"
0:25 and it will actually say it over and over and over
0:27 so you can get really excited and say
0:29 "Good morning Jeff, Good morning Jeff, Good morning Jeff",
0:32 and in fact this one will do that 3 times,
0:34 this one on the other hand will just say "Good day Michael", one time.
0:37 However, there is a problem with these down here,
0:39 we might like to write code like this,
0:41 we might just like to say well, let's just greet Mark,
0:44 and there might be some way to write code that there is a default greeting
0:48 to be given like "Hello", and the number of times might be 1
0:51 so like if we don't pass the greeting and the number of times,
0:56 we'd like to just say "Hello Mark", one time.
0:58 But if we say "Mark" and "Good afternoon"
1:00 we'd like it to say "Good afternoon Mark" one time.
1:03 Of course we can specify all the details and say "Good afternoon Mark" twice.
1:07 How do we go about this in Python?
1:09 Well, we use default values.
1:11 First of all, it's not going to love it the way it is,
1:13 you can see PyCharm's indicating issues and if we'd run it you will find them,
1:17 the first part worked just fine, "Good morning Jeff" and then did that 3 times,
1:21 the little error snuck in between there, and then it said "Good day Michael"
1:25 and then we started hitting the trouble.
1:27 Click here and it will take you right to the line of trouble,
1:29 yeah that's the one we expected.
1:31 So we can actually go up here and instead of having different signatures,
1:34 we can have default values, so if we'd like to have default greeting
1:38 we can come over here and we can say, right in line, "Hello".
1:42 So if we wanted to say Hello- name,
1:45 if you don't specify it, then we can do it this way.
1:48 Now, this would almost fix this line, except for we still need to deal with times
1:51 and the default values have to go after all the non-default values, so let's set this to 1,
1:56 that seemed reasonable; now, this actually fixes all of these errors, let's go.
2:01 Perfect, look, if this one here "Hello Mark",
2:05 this one said "Good afternoon Mark" one time,
2:08 this one said "Good afternoon Mark" two times, like so.
2:12 Finally with these default values we can actually put them in any order we want,
2:16 here we don't say the name of them we just say "Good afternoon
2:18 and two", we use them as positional values but I could say something like this,
2:22 I could say greeting is "Yo!", name is "Michael" and times is 4.
2:30 So we can put these in any order we want using keyword arguments
2:33 and we get "Yo Michael" 4 times.
2:37 More importantly, this lets us two things like skip over,
2:41 let me keep this here for you, more importantly his lets us skip over
2:45 some of the default values, so here I can say name is Michael,
2:48 skip over greeting and use its default "Hello" and then let's just say 2 times here.
2:53 So they should say "Hello Michael" 2 times at the end.
2:56 "Hello Michael", 2 times at the end.
2:59 All right, so in Python default values play a really important role
3:03 in doing what method overloading based on signature might have done
3:07 in other circumstances.
3:09 We'll see some more Pythonic ways to deal with this as well,
3:12 some other functional techniques we can use.
3:15 So, specifying default values here, lets us perform what in languages
3:19 like C# and C++ often are done through signature overloading
3:24 and having multiple methods that are distinguished by signature.
3:28 However, there is a warning here,
3:30 so for these default values there are some extremely serious gotchas
3:34 and certain circumstances will look at those at the end.