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


Talk Python's Mastodon Michael Kennedy's Mastodon