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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *