For supporting operations like obj[key], the class should define the _______ method.
- __getitem__
- __getkey__
- __getvalue__
- __index__
To support operations like obj[key] for custom objects, you should define the __getitem__ method in the class. This method is called when you use square brackets to access an item in the object.
How can you make a single class work with multiple with statement contexts simultaneously?
- By implementing both enter and exit methods
- By nesting with statements inside each other.
- By using the 'as' keyword followed by a custom context manager.
- This is not possible in Python.
To make a single class work with multiple with statement contexts simultaneously, you need to implement both the enter and exit methods in the class. This allows you to manage multiple contexts effectively.
If a context manager's enter method raises an exception, what happens to its exit method?
- It depends on Python's version.
- It's called after enter regardless of exceptions.
- It's called only if enter completes without exceptions.
- It's not called at all.
If the enter method raises an exception, the exit method is not called at all, ensuring resource cleanup doesn't happen in case of an error during setup.
Which method in a class is responsible for deleting an attribute?
- del()
- delete()
- destroy()
- remove()
The 'del()' method is used to delete an attribute or perform any necessary cleanup when an instance of a class is about to be destroyed.
Which feature of Python allows us to handle different numbers and types of arguments?
- Default arguments
- Function overloading
- Operator overloading
- Variable-length arguments
Variable-length arguments (e.g., *args and **kwargs) allow Python functions to accept a varying number of arguments, enabling flexibility in function calls.
What keyword is used in Python to make a decision?
- choice
- decide
- else
- if
In Python, the 'if' keyword is used to make a decision. It allows you to execute a block of code conditionally based on the evaluation of a given expression.
In Python, how can you execute a block of code multiple times without using a loop?
- List Comprehension
- Recursion
- exec() function
- goto statement
In Python, you can execute a block of code multiple times without using a loop by using recursion, where a function calls itself. This allows for repeated execution without an explicit loop construct.
A Python script unexpectedly terminates, and upon investigation, you discover a circular import. What could be a probable solution?
- Enable Circular Imports
- Reorder the Import Statements
- Use the __init__.py File in Packages
- Use the sys.modules Dictionary
Circular imports occur when two or more modules import each other. To solve this, reorder the import statements so that they don't create a circular dependency. You may also use the __init__.py file in packages to control imports.
In the context of Python packages, what is the significance of a directory containing an init.py file?
- It indicates a directory is empty and serves no purpose.
- It is only used for version control purposes.
- It is used to exclude the directory from package scanning.
- It marks the directory as a special package directory.
In Python, the presence of an __init__.py file in a directory signifies that the directory should be treated as a Python package. It allows you to organize related modules into a package, and it also enables the use of relative imports within the package. Without this file, Python won't recognize the directory as a package.
In Python, the operator == checks for _______.
- Assignment
- Identity
- Inequality
- Value equality
The operator '==' checks for value equality. It returns True if the values on both sides of the operator are equal.