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.
0:04 Here we have two methods called simple on this class,
0:07 now the first one takes no parameters,
0:10 the second one takes a details parameter,
0:13 some languages these would be two distinct methods
0:16 and based on the particular signature you are trying to use,
0:19 the compiler would select one or the other.
0:21 This does not exist in Python, let's look at it in an example.
0:25 Here we have the same basic code
0:27 and we are creating what I call the sample class,
0:30 that really doesn't mean anything, and we are going to call a simple method on it,
0:34 notice we have this kind, we have this kind.
0:37 PyCharm is giving us a little bit of clue that something is going wrong here,
0:41 you can see it's highlighting the second simple on line 6
0:43 but let's go ahead and run it and just see what happens.
0:47 So look, the first one where we pass some details, this actually worked,
0:51 I call this simple with details and it said some details.
0:55 However the second one didn't work,
0:57 simple is missing one required parameter, details,
1:01 so what's going on here,
1:03 it turns out there can only be one method called simple on this class,
1:07 and so when we define the second one
1:09 we basically eject this one from the class,
1:12 we just overwrite it in a dictionary that the key simple now means something different,
1:16 and so even though PyCharm is little bit freaked out by this here
1:21 because it were doing it wrong above, did catch that error,
1:24 this is the one that is actually not going to work because as far as this class is concerned,
1:28 there is no method simple that takes no parameters,
1:31 oh look, in the subsequent sections on how Python deals with this,
1:36 because this kind of flexibility is super powerful and Python does support it
1:40 it just doesn't support it in the traditional method overloading way
1:44 that you might be familiar with coming from C++, Java, CSharp and so on.
1:49 In a graphic, here it is. Here is our class, we have the two methods,
1:52 the top method is being overwritten or ejected by the bottom one,
1:56 these could just as well be functions and not methods it would have the same effect,
2:00 but what is really important to notice is
2:03 when I ran the code it wasn't the fact the I was redefining simple
2:07 that was actually causing the runtime problem,
2:09 this code as it's on the screen here, this will run perfectly fine
2:13 it just won't do what you think so be very careful here.
2:16 So for example, if we create the sample class,
2:20 we call a simple method with details, the last one in our list that is going to work correctly,
2:24 but the bottom one crashes because it no longer exists basically.
2:29 Now, there is nothing specifically Pythonic about this,
2:32 but this lays out the problem the next 3 or 4 lectures
2:36 are going to show as Pythonic ways to solve this problem
2:39 that do not have to do with method overloading and signature matching.