Issue
I made a file named challenge.py
with this code in it:
import challenge
def main():
print('Circular much...')
challenge.main()
From this I was expecting python to raise an error due to the circular import of importing the file which is running but I found that on python 3.7 & 3.8 this file runs and prints out Circular much...
twice. I would understand once as that would mean that the rest of the file when it is importing itself isn't running and I would understand a recursion error as it ran challenge.main() infinitely down the stack but I don't understand why it prints it twice and stops?
Solution
Tracing this through:
import challenge
Ok, we'll import
challenge.py. Here we go...
import challenge
We're already importing challenge.py, so we won't do it again.
def main():
print('Circular much...')
Defined the function main()
in the namespace challenge
. Cool.
challenge.main()
Now call the function main()
in the namespace challenge
. That prints Circular much.... There's your first print.
Now we're back in the main module again.
def main():
print('Circular much...')
This defines the function main()
in the global namespace (which never gets called).
challenge.main()
This calls the function main()
in the namespace challenge
, again printing Circular much....
And we're done. Two prints of your message.
Does that help?
Answered By - Fred Larson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.