Adding Objects to a Namespace
00:00
Before we wrap up this lesson, let me show you one more cool thing. Any new variables or functions that you add to adder.py
will be accessible in main.py
without you having to import anything new.
00:13
So open the editor window for adder.py
and add the following function below the function definition of add()
. def
double(x):
and then as the function body return x + x
00:31
Save the adder.py
file and then open the editor window for main.py
and add the following code after your print()
call: double_value = adder.double()
And then you pass in value
as the argument.
00:50
That’s the variable you defined two lines above. And then in the next line, you do a print()
function call for double_value
. Now, save and run main.py
.
01:04
When you run the module, there is no error, and the values 4
and 8
are displayed in the interactive window. So since double()
already exists in the namespace, no NameError
is raised and you can use it right away. That’s pretty convenient.
01:21 And if you aren’t convinced yet why modules are handy, let me show you a slide with some bold words on it.
Become a Member to join the conversation.