Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Order Your Functions

00:00 I guess one more thing we can do here is we can move the main() function to the top, after the class definition of the custom exception. That gives, if someone goes to read the code, that also gives them a quicker idea of what’s happening.

00:16 This, of course, would make more sense now if we had more functions, but I see this more as a little hint for maybe the original codebase, where you would want to give a quick overview of what’s happening. And in this case, the main() function packages everything together that happens here.

00:31 And so it’s nice to have it up top so that it’s quick and easy to read.

00:36 And maybe this is also an interesting thing to talk about because people might not want to put the main() function on the top because it’s calling the validate() function. And in Python, the entry point of a script is always on the top.

00:49 So you might expect that you get a problem that validate() isn’t defined yet, but when Python runs the script, it just defines the functions and it doesn’t run any code in here before you actually call any of those functions.

01:03 So there’s no issue here because we are running the main() function only in line 17, and at that point, the validate() function on line 12 is already defined.

01:12 So main() knows what to do when this piece of code on line 9 finally runs.

01:18 Okay, so to verify this, you may run this again. Of course. So it’s not just your word.

01:26 Still going to run into the same error: SourceEqualsTargetError. Okay. Things working the same.

Become a Member to join the conversation.