PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 40
Q688 - Modules
What in true about the built-in dir() mechanism
in the context of modules and packages?
-
A
It is a list contained by a module reflecting the module contents.
-
B
It is a dictionary contained by a module reflecting the module contents.
-
C
It is a function which can be invoked from within a module
in order to obtain the module contents.
-
D
It is a function which can be invoked with a module
passed as an argument in order to obtain the module content.
Reveal correct answer
Correct answer: D
Explanation
Topics: dir() modules
Try it yourself:
- import math
- print(dir(math))
- """
- ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
- 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb',
- 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp',
- 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma',
- 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt',
- 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan',
- 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin',
- 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
- """
Explanation:
Yes, you can pass a module to the dir() function
and it will return all attributes of that module.
The dir() function returns all properties
and methods of the specified object, without the values.
This function will return all the properties and methods,
even built-in properties which are default for all object.
https://www.w3schools.com/python/ref_func_dir.asp
Q688 (Please refer to this number, if you want to write me about this question.)
A. This statement is incorrect because the dir() function does not return a list contained by a module. It returns a list of valid attributes for the module, such as functions, classes, variables, and submodules.
B. This statement is incorrect because the dir() function does not return a dictionary contained by a module. Instead, it returns a list of valid attributes for the module.
C. This statement is incorrect because the dir() function is typically called with a module passed as an argument from outside the module to obtain its contents. It is not typically called from within the module itself.
D. The built-in dir() mechanism is a function that can be called with a module passed as an argument. It returns a list of valid attributes for that module, including functions, classes, variables, and submodules.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
