You are writing a nested function, and you want to modify a variable from the outer function. However, you don't want this change to affect the global scope. How can you achieve this?
- Use the global keyword
- Use the local keyword
- Use the nonlocal keyword
- Use the private keyword
To modify a variable from an outer function without affecting the global scope, use the 'nonlocal' keyword. It allows you to access and modify the variable in the closest outer scope.
Loading...
Related Quiz
- How can you include JavaScript functionality in a web page developed using a Python web framework?
- The ____ algorithm is used to traverse all the vertices of a graph in depthward motion.
- In a series of if, elif, and else, how many times can elif appear?
- Which Python data structure is suitable for storing multiple data types and allows duplicate elements?
- You are assigned to optimize a Python application performing extensive calculations. Which approach would you take to reduce the computational time and improve the efficiency of the calculations?