Most people forget that python is both procedural and Object Oriented. If you find yourself wanting to use a procedural, Bash style of programming with Python, take a look at the global statement.

It lets you do something like this, because without the :


In [34]: x = 1; y = 2

In [35]: def func():
   ....:     global x, y
   ....:     print "X = %s, Y = %s, in func" % (x,y)
   ....:
   ....:     

In [36]: func()
X = 1, Y = 2, in func

Here is an even better example, based on the comment by Ant, as the top example was too vague, probably because I was trying to make the example too "tiny" :) That is a challenge, to make a tip tiny, yet explicit.


In [46]: x = 1; y = 2; z = "out here"

ln [47] def func():
   ....:         global x,y,z
   ....:         print "X = %s, Y = %s, in func.  Z says: %s " % (x,y,z)
   ....:         x = 1000;y=1000;z="in here"
   ....:         print "X = %s, Y = %s, in func.  Z says: %s " % (x,y,z)

In [48]: func()
X = 1, Y = 2, in func.  Z says: out here
X = 1000, Y = 1000, in func.  Z says: in here 

I should also say, thanks to Tres, who showed me this tip to begin with!