http://www.skillbrew.com /Skillbrew Talent brewed by the industry itself Namespaces, global variables and Docstrings Pavan Verma @YinYangPavan Founder, P3 InfoTech Solutions Pvt. Ltd. 1 Python Programming Essentials
© SkillBrew http://skillbrew.com Namespaces 1. Python uses what are called namespaces to keep track of variables. 2. A namespace is just like a dictionary where the keys are names of variables and the dictionary values are the values of those variables. 2
© SkillBrew http://skillbrew.com Namespaces Local Namespace Global Namespace Built-in Namespace specific to the current function or class method specific to the current module global to all modules 3
© SkillBrew http://skillbrew.com Order of a lookup Local Namespace Global Namespace Built-in Namespace def printList(upper_limit, step=2): # variables upper_limit and step belong to this function’s local namespace. print "upper limit: %d" % upper_limit num_list = range(0, upper_limit, step) print num_list printList(upper_limit=5, step=2) a = "foo" b = "bar" # variables a, b and function printList belong to Global namespace of this module 4
© SkillBrew http://skillbrew.com Global variables counter = 10 def printVar(): print counter printVar() print "counter: %d" % counter Output: 10 counter: 10 Variable defined outside a function or class is a global variable. Counter is a global variable 5
© SkillBrew http://skillbrew.com Global variables counter = 10 def printVar(): counter = 20 print "Counter in: %d" % counter printVar() print "counter out: %d" % counter Output: Counter in: 20 counter out: 10 counter in function definition is local variable tied to function’s namespace. Hence the value of global counter does not gets modified. 6
© SkillBrew http://skillbrew.com Global variables counter = 10 def printVar(): global counter counter = 20 print "Counter in: %d" % counter printVar() print "counter out: %d" % counter Output: counter in: 20 counter out: 20 In order to use the global counter variable inside the function you have to explicitly tell python to use global variable using global keyword. 7
© SkillBrew http://skillbrew.com Docstrings def add(x, y): """ Return the sum of inputs x, y """ return x + y print add.__doc__ Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. docstri Access a docstring using object.__doc__ 8
© SkillBrew http://skillbrew.com Resources  http://www.ibiblio.org/g2swap/byteofpyt hon/read/local-variables.html  http://www.ibiblio.org/g2swap/byteofpyt hon/read/docstrings.html 9

Python Programming Essentials - M19 - Namespaces, Global Variables and Docstrings

  • 1.
    http://www.skillbrew.com /Skillbrew Talent brewed bythe industry itself Namespaces, global variables and Docstrings Pavan Verma @YinYangPavan Founder, P3 InfoTech Solutions Pvt. Ltd. 1 Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.com Namespaces 1.Python uses what are called namespaces to keep track of variables. 2. A namespace is just like a dictionary where the keys are names of variables and the dictionary values are the values of those variables. 2
  • 3.
    © SkillBrew http://skillbrew.com Namespaces Local Namespace Global Namespace Built-in Namespace specificto the current function or class method specific to the current module global to all modules 3
  • 4.
    © SkillBrew http://skillbrew.com Orderof a lookup Local Namespace Global Namespace Built-in Namespace def printList(upper_limit, step=2): # variables upper_limit and step belong to this function’s local namespace. print "upper limit: %d" % upper_limit num_list = range(0, upper_limit, step) print num_list printList(upper_limit=5, step=2) a = "foo" b = "bar" # variables a, b and function printList belong to Global namespace of this module 4
  • 5.
    © SkillBrew http://skillbrew.com Globalvariables counter = 10 def printVar(): print counter printVar() print "counter: %d" % counter Output: 10 counter: 10 Variable defined outside a function or class is a global variable. Counter is a global variable 5
  • 6.
    © SkillBrew http://skillbrew.com Globalvariables counter = 10 def printVar(): counter = 20 print "Counter in: %d" % counter printVar() print "counter out: %d" % counter Output: Counter in: 20 counter out: 10 counter in function definition is local variable tied to function’s namespace. Hence the value of global counter does not gets modified. 6
  • 7.
    © SkillBrew http://skillbrew.com Globalvariables counter = 10 def printVar(): global counter counter = 20 print "Counter in: %d" % counter printVar() print "counter out: %d" % counter Output: counter in: 20 counter out: 20 In order to use the global counter variable inside the function you have to explicitly tell python to use global variable using global keyword. 7
  • 8.
    © SkillBrew http://skillbrew.com Docstrings defadd(x, y): """ Return the sum of inputs x, y """ return x + y print add.__doc__ Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. docstri Access a docstring using object.__doc__ 8
  • 9.
    © SkillBrew http://skillbrew.com Resources http://www.ibiblio.org/g2swap/byteofpyt hon/read/local-variables.html  http://www.ibiblio.org/g2swap/byteofpyt hon/read/docstrings.html 9