Bird
0
0

Why might this code cause an error?

medium📝 Debug Q7 of 15
Python - Modules and Code Organization
Why might this code cause an error?
from mymodule import *
print(myfunc())

Assuming myfunc is defined in mymodule.py.
AUsing * import is not allowed in Python
Bmyfunc is not called with the module prefix
CThe function myfunc is not imported because it is not listed in __all__ in mymodule
DThe file mymodule.py is missing
Step-by-Step Solution
Solution:
  1. Step 1: Understand star import behavior

    Using 'from module import *' imports only names listed in __all__ if it exists.
  2. Step 2: Understand __all__ effect

    If __all__ is defined but does not list 'myfunc', the function is not imported with '*', causing NameError.
  3. Final Answer:

    The function myfunc is not imported because it is not listed in __all__ in mymodule -> Option C
  4. Quick Check:

    Star import depends on __all__ [OK]
Quick Trick: Use explicit imports or define __all__ for star imports [OK]
Common Mistakes:
  • Thinking star import is disallowed
  • Assuming functions are always imported with *
  • Confusing missing file with import error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes