Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Rename Your Symbols

00:00 Okay, now with having cleaned up the code a bit more and actually answered the question, let’s do a little bit of more refactoring because there are a few instances in this code where we can optimize the code even a bit more.

00:14 It might not be particularly needed to get an answer to your question, but it’s still nice to end up with a code example that might be a bit more best practice. So Martin, what’s something that catches your eye right away where you would do a little bit of refactoring?

00:31 The first thing that’s been bugging me since I’ve wrote it here is this X. This is definitely not a very useful name for this minimal code example that we have now. So I will just call it input—no, user_input. Sure. Yeah, I think user_input is fine. user_input. We don’t really know what what it does.

00:49 You were stopping for a moment calling it input. Yes. What was your thinking there?

00:55 So I didn’t want to call it input because that’s the name of a built-in function. You should always avoid calling something something that already exists in Python. Like you have this—

01:09 let’s make some space here. If I do helphelop [laughs]—help(input), then you can see that this is a built-in function that already exists.

01:20 So if I would name this here input, then I’m actually overriding this. So you definitely don’t want to do that. Yeah. So the code would still work, but if you know that input is a built-in function, it’s good to adjust a little bit, for example, to user_input to not override the built in function.

01:37 And so what I can do here, actually I can just go and use something that the code editor provides, and most of code editors do that. I can, in VS Code, right-click after highlighting the variable that I want to change and click on Rename symbol, and then I can get this pop-up here and can write user_input.

01:57 And then it’s going to replace it in all of the occurrences that it identifies as the same. So this is user_input, and here I’m passing it as an argument to main().

02:07 And then you see, like, it didn’t refactor it in here because here we’re in a different scope, in the main() function. This is the parameter that’s passed to main(), so we want to refactor that too.

02:17 And I’m just also going to call it user_input here. Right-click and Rename symbol.

02:22 And this doesn’t need to be the same name as I’m calling it outside, but also in this case it makes sense.

02:30 So the main() function takes user_input, and then the load_model() function takes two values from the user_input dictionary—one "s" and one "t", currently. But I’m also not very happy with those. And here, luckily we have some context here—it’s source and target.

02:47 So I’m just going to rename these to source and target.

02:53 And I will have to change these as well …

02:58 "target". It’s a little more descriptive. Okay, so now you have a dictionary with a descriptive name, and inside of the dictionary you have two keys—"source" and "target"—that before were "s" and "t", and now it actually has a verbose name where it’s easier to understand what the value should be of them. So currently in line 13, "source" has a value 0 and "target" has a value 0. Right, and that means for us that this is going to throw the error, so let’s look at the error next.

Become a Member to join the conversation.