PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 7
Q354 - Modules
A predefined Python variable, storing the module name, is called:
-
A
__module__ -
B
__mod__ -
C
__name__ -
D
__modname__
Reveal correct answer
Correct answer: C
Explanation
Topic: __name__
Try it yourself:
- # First execute the following to create the needed file:
- with open('test.py', 'w') as f:
- f.write('print(__name__)')
- print(__name__) # __main__
- import module # module
- # print(__mod__) # NameError: ...
- # print(__module__) # NameError: ...
- # print(__modname__) # NameError: ...
Explanation:
The name of the predefined variable is __name__
In the current file the output will be __main__
In an imported file named module.py the output would be module
The others do not exist.
(There is another question about the same topic Q153.)
Q354 (Please refer to this number, if you want to write me about this question.)
A. The choice __module__ is incorrect. While it may seem similar to the correct choice __name__, there is no predefined Python variable named __module__ that stores the module name. It is not a valid variable in Python for this purpose.
B. The choice __mod__ is incorrect. There is no predefined Python variable named __mod__. It is not used to store the module name in Python programming.
C. The correct choice is __name__. In Python, the __name__ variable is a predefined variable that stores the name of the current module. It is commonly used to check whether a module is being run as the main program or being imported into another module.
D. The choice __modname__ is incorrect. There is no predefined Python variable named __modname__ that is used to store the module name. It is not a recognized variable in Python for this specific functionality.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
