I try to understand how python works and I need a small explanation.
So I wrote a very short example and I have trouble understanding why it doesn't work.
I create a test.py
:
def a():
print('a() try to call non existing b()')
b()
At this stage, if I write in a python shell
>>> import test
>>> test.a()
It doesn't work and it is normal because b()
is unknown.
But when I write these following lines, it still not work.
>>> import test
>>> def b():
... print('b()')
...
>>> test.a()
A function in a python module can only call a function in the current module and imported modules ?
You would have to define b()
within the same test.py
where you defined a()
.
It would work if you created a new python module (python file) where b()
is defined and then imported that module into test.py
from another_module import b # refers to function b
def a():
print("this function calls b")
b()
Something like the one above would work. Remember that the module that contains function b()
and the test.py
module should be in the same directory for it to work.