Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Methods and Functions
Lecture: There is no method overloading in Python

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In Python, there is no function or method overloading. Here we have two methods called simple on this class, now the first one takes no parameters,
0:11 the second one takes a details parameter, in some languages, these would be two distinct methods
0:17 and based on the particular signature you are trying to use, the compiler would select one or the other.
0:22 This does not exist in Python, let's look at it in an example. Here we have the same basic code and we are creating what I call the Sample class,
0:31 that really doesn't mean anything, and we are going to call a simple method on it, notice we have this kind, we have this kind.
0:38 PyCharm is giving us a little bit of clue that something is going wrong here, you can see it's highlighting the second simple on line 6
0:44 but let's go ahead and run it and just see what happens. So look, the first one where we pass some details, this actually worked,
0:53 I call this simple with details and it said "Some details." However the second one didn't work, "simple() is missing 1 required parameter: 'details'",
1:02 so what's going on here? It turns out there can only be one method called "simple" on this class, and so when we define the second one
1:10 we basically eject this one from the class, we just overwrite it in a dictionary that the key "simple" now means something different,
1:18 and so even though PyCharm is little bit freaked out by this here because it were doing it wrong above, did catch that error,
1:25 this is the one that is actually not going to work because as far as this class is concerned, there is no method "simple" that takes no parameters,
1:32 oh look, in the subsequent sections on how Python deals with this, because this kind of flexibility is super powerful and Python does support it,
1:41 it just doesn't support it in the traditional method overloading way that you might be familiar with coming from C++, Java, C# and so on.
1:50 In a graphic, here it is. Here is our class, we have the two methods, the top method is being overwritten or ejected by the bottom one,
1:57 these could just as well be functions and not methods it would have the same effect, but what is really important to notice is
2:04 when I ran the code, it wasn't the fact the I was redefining "simple" that was actually causing the runtime problem,
2:10 this code as it's on the screen here, this will run perfectly fine it just won't do what you think, so be very careful here.
2:17 So for example, if we create the "Sample" class, we call a "simple" method with details, the last one in our list that is going to work correctly,
2:26 but the bottom one crashes because it no longer exists, basically. Now, there is nothing specifically Pythonic about this,
2:33 but this lays out the problem the next 3 or 4 lectures are going to show us Pythonic ways to solve this problem
2:40 that do not have to do with method overloading and signature matching.

Talk Python's Mastodon Michael Kennedy's Mastodon