What's the difference between global and nonlocal keywords when referring to variables in a function?
- 'global' is used to declare a variable as local to the current function.
- 'nonlocal' is used to declare a variable as accessible only within the current function.
- 'nonlocal' is used to declare a variable as global throughout the program.
- global' is used to declare a variable as accessible from any function in the program.
In Python, 'global' is used to declare a variable as accessible from any function in the program, while 'nonlocal' is used to declare a variable as accessible only within the current function or nested functions. These keywords help control variable scope.
Loading...
Related Quiz
- Which of the following is not a built-in exception in Python?
- How can you handle an exception for an error caused by dividing by zero in Python?
- In Django Rest Framework, how would you implement token-based authentication?
- You are debugging a failing test in pytest and need to inspect the values of variables at the point of failure. Which pytest option would you use to achieve this?
- How can you create a custom exception class in Python?