Intermediate LevelPython
How do you inspect Python runtime internals, command-line arguments (sys.argv), and path entries using the sys module?
❓ Interview Prompt: Explain `sys` module, `sys.argv`, `sys.path` (module search paths), `sys.getsizeof()` (object memory usage), and `sys.exit()`.
The 'sys' module provides access to variables used and maintained by the CPython interpreter:
Key Attributes:
•
'sys.argv': List of command-line arguments passed to script.
•
'sys.path': Array of directory paths searched when importing modules.
•
'sys.getsizeof(obj)': Returns size of an object in bytes.
•
'sys.exit(code)': Terminates current process with status code.
Code Implementation Example
python
import sys
print('Python Version:', sys.version)
print('Command Args:', sys.argv)
print('Module Search Paths:', sys.path[:2])
print('Memory Footprint of list:', sys.getsizeof([1, 2, 3]), 'bytes')💡Inspecting runtime environment and object memory size via sys.
🎯 Interview Key Takeaway
Use the sys module to inspect runtime interpreter settings, CLI arguments, and object memory footprints.
#sys#Introspection#sys.argv#sys.getsizeof#Intermediate
Back to all Python Questions→