Accessing Objects From Inside a Module
00:00 Variables, functions, and classes in a module can be accessed from within the same module by just typing their name. Let me show you what I mean by that.
00:09
If you add the two lines of code that you used in main.py
in adder.py
and then run the code, then the code should work. So at the end of adder.py
, add the line value =
add(2, 2)
. On the next line, print(value)
.
00:28
And then you run it, then it works. And the reason for this is because you are accessing add()
from within the same file. They’re in the same module, and therefore in the same namespace.
00:42
So that was just an example for the namespace inside of one module. Remove both lines of code, the value
variable definition, and the print()
function call, save the file, and get back to main.py
.
00:54
So adder.py
should look like it was at the beginning with only the add()
function definition.
Become a Member to join the conversation.