Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python function overloading

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's see function overloading in Python.
0:03 Want to start without with our
0:04 standard structure here like this.
0:07 And in here, remember, what we had in C# was
0:10 calling it with different parameters.
0:11 I'm just going to paste a bunch of coding here
0:13 so we have something really, really similar, okay?
0:16 So we're going to call it, say_hello, say_hello("Zoe")
0:19 say_hello("Zoe", 5), I think I updated this
0:21 to have four, so let's do that, and so on.
0:26 Now, we're going to comment that one out for a sec
0:30 let's define this function that we're working with here.
0:33 def say_hello. And then what do we want to do?
0:38 Well, let's put a little print statement here
0:39 that just prints out the values.
0:45 First it'll say, Hello there.
0:47 So turns out that's going to work great for this one
0:50 but PyCharm is kind of warning us
0:52 maybe something's not super about this.
0:55 Right, unexpected argument, unexpected argument, and so on.
0:58 Let's run it, we'll get through a little bit.
1:01 That said, say 'Hello, hi there, hello name'
1:04 and then it crashes because we got to the Hello name
1:07 in that one, right there, that's not working.
1:10 So how do we add this?
1:13 In C#, what we do is we had an empty one, like this
1:17 and we said, want to call all the say hello one with friend
1:22 and we had a name over here, Hello, name, like that.
1:27 It turns out Python does not have this concept
1:30 of two functions with the same name but different parameters
1:34 and see here, there's this problem and it ends up
1:38 it's going to say basically only one of these is going to survive.
1:41 And what actually happens is there's not even an error
1:43 this is really super frustrating about Python
1:45 this definition of that say hello simply replaces that one
1:49 because it's later in the file.
1:50 Think this is a shortcoming in the language
1:52 but anyway, what you're going to find out
1:54 is it's as if the code was only that.
1:57 Well then obviously this line doesn't make any sense.
2:00 So this process of how we did it in C#
2:02 doesn't even make any sense, it's going to go the wrong way.
2:04 So what we can do here instead is we can say
2:06 This is friend. Okay?
2:09 So we'll have our default value
2:11 but when it's like that, it'll be friend
2:12 and this one hello, so that's kind of what we did in C#
2:15 but we did two functions, now we're just doing one
2:18 with a default argument.
2:19 Could've done it that way in C#
2:20 but I wanted to show you more of what's going on.
2:23 say_hello, it says Hello, friend
2:24 say_hello("Zoe"), it says Hi, Zoey
2:27 the next one crashes right there.
2:30 So this next one is, this is how many times
2:32 we want it to say hello or something like that
2:34 so let's... in here we have another parameter
2:36 and we have times=1
2:38 and that solves that problem.
2:41 That's pretty cool.
2:42 So with times equals times...
2:47 And that'll get us farther, as we'll see.
2:49 So it gets us to this, but again
2:51 when we call up this one, it died.
2:53 And we just click here to go right to that line.
2:56 How do we deal with this variable number of arguments?
2:59 Well, in Python, we-- C#, let's step back
3:02 we had params, object array, args.
3:08 Python has the exact same concept
3:09 but they don't like all those words
3:11 so they just put *.
3:13 And we'll say args equals args, like this.
3:19 Python just prints arrays
3:20 I don't have to write print array here, that's pretty sweet
3:22 so if I run this again, I can see the args are like this.
3:27 Let's do one more thing here
3:29 let's suppose in this one we wanted
3:31 actually do one more line, like this.
3:34 Let's suppose we wanted to say val=7
3:37 mode=prod, something like that.
3:42 If we want to write code like that, here
3:45 yeah, I think I'll be able to make it work.
3:48 Line that order, come down here like this.
3:50 We have to come in and say this takes
3:53 also arbitrary keyword argument
3:55 so here's our arbitrary positional arguments.
3:57 So the way you say that in Python
3:58 is you say, **kwargs, like this.
4:02 Now that's happy up there
4:04 and we can print kwargs={kwargs}.
4:07 This becomes a dictionary where the
4:09 keys val, value 7, keys mode, value prod.
4:14 Run it one more time.
4:16 Beautiful, look at that, it's working like a champ.
4:18 So here's that additional bit.
4:20 So this is an additional level of flexibility
4:23 of passing arbitrary arguments I don't think C# has
4:26 I don't remember it having, maybe it does
4:28 but they definitely have this, but this is also super nice
4:32 to be able to take additional keyword arguments
4:35 on top of just the positional ones.
4:38 So we could actually pretty much implement this one as well
4:41 and it's, I'm not going to
4:43 it says basically the same thing, so we're going to drop it.
4:45 Here we go, this lets us have
4:47 arbitrary positional arguments
4:48 arbitrary keyboard arguments
4:50 default values for these, and these are positional ones.
4:54 That looks pretty good to me.
4:57 You may remember, there were two other ones in C#
5:01 that was like this, where you said say 'hello integer'
5:04 and it did something different
5:06 and it said say 'hello decimal'
5:08 and it did something different.
5:10 Technically speaking, we could
5:12 do something stupid like this
5:14 we could say, if is instant, name, and what've we got here
5:21 an int, print, all right, could do print int version
5:26 da da da da da, else.
5:30 Technically possible, I think it's a bad idea.
5:33 I would not, in fact I'm going to delete this.
5:35 Just not a good idea, I wouldn't do it.
5:37 Basically this type of overloading by type
5:40 under the one argument, and it's the type
5:43 that determines which function gets called
5:45 it's not something that works in Python.
5:47 But all the other types, the default values
5:50 the void ones, the additional parameters
5:53 the named ones, all that kind of stuff
5:55 works really similarly to the way you'd expect it does
5:58 in C#, but this type, this last two here
6:01 this type of overloading just doesn't exist
6:03 'cause you don't base which function you call on types
6:06 right, it's just not how it works.