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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Persevere Through Obstacles

00:00 So far, we have looked at the code itself, our first impression, and the first impression of the question, which started with a how, so we try to focus more on improvements that we can do on the code itself to make it work.

00:16 And maybe we can start with running the code like it is right now and then going into the cleanup. What do you think, Martin? That sounds good.

00:26 We mentioned already before that Pylance tells us that there’re, like, an undefined variable. You might expect that there’s going to be an error here. So if I run the original file, there’s currently nothing happening, which is just because in the code snippet that we got here, there’s no function call.

00:44 So Python just defines these two functions but doesn’t actually run any code. But if I would now go ahead and call main(),

00:52 you see that Pylance in line 29 already tells us again that, well, there’s actually two things that’s missing—a parameter X. The main() function needs something, and I think that’s very good that you’re kind of, like, you’re just hitting an obstacle. You’ve got this code, you want to help, and then you’re like, “Oh wait, actually I have to put something in.” So now you have to try to understand what this X is that the main() function has there. Good point.

01:19 So my thought process was I’m going to run this and I know I’m going to run into an error because of line 25, but I don’t even get to that. Like I’m going to run into another error before, which is that main() is missing one required positional argument, X.

01:32 And that’s just like Philipp said, I’m just confronted with a bunch of obstacles to actually get to the point of helping this person with a question. Let’s go ahead and try to make this a working example.

01:45 I’m going to start with keeping the naming of this variables, which this is not a great name and we’ll refactor it later, but for now I just want to get it to work. Yeah.

01:55 And so I need to figure out what is X, right? So I need to look into this code and see where is it used. And in that case it looks like it’s a dictionary structure where the person is performing some look-up on two keys, "s" and "t", which means that I’m going to here have to have a dictionary that has, let’s say, "s" and that points to something. I have no idea what it points to.

02:17 I’m just going to say 0,

02:21 and "t" is going to point to 1.

02:25 Like, I don’t know what X is. I’m just trying to figure out from the code what is the data structure I need to actually get this to run. Okay, now I’m passing this in as X and then I can try running it again.

02:40 And now I’m actually not running into any error, so that’s good.

02:45 And why is that? Because load_model()

02:53 returns, so— Yeah, that’s going into the more details of the code. So one thing that happens here in line 22 is you’re starting with a try statement, and you are catching the exception in line 26, like any exception basically.

03:11 So my guess would be if anything in this code now throws an exception, we won’t see it because it’s in this tryexcept block. Nice.

03:20 What we can see here is also, like, that if you use this very general exception, you catch all of the exceptions. So what we ran into now is that this person’s probably trying to catch some of these exceptions that are happening inside of load_model(), but it’s also catching the exception that’s raised here, that you have, like, an undefined variable. So we don’t even come to this error, but it’s still a problem in the code basically that makes it not run the way that we would want to.

Become a Member to join the conversation.