Rules for local and global variables in python?

  • In python, the variables referenced inside a function are global.
  • When a variable is assigned new value anywhere in the body of a function then it is assumed as local.
  • In a function, if a variable ever assigned new value then the variable is implicitly local and explicitly it should be declared as global.
  • If all global references require global then you will be using global at anytime.
  • You’d declare as global each reference to built-in function or to component of module which is imported.
  • The usefulness of global declaration in identifying side-effects is defeated by this clutter.

No comments:

Post a Comment