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