[Tutor] Re: Newbie OOP Question. (diff between function, modu le and class)

alan.gauld@bt.com alan.gauld@bt.com
Tue, 11 Jun 2002 14:16:43 +0100


> > system doesn't have a syntax for referring to the current 
> module's parent
> > (no "..").
> Ahh. Now that may prove interesting. Would it be beneficial to make a
> reserved command like .. That pops you back into the parent namespace?

There is sort of its the global statement:

z = 42

def f(x):
   z = 2*x
   return z

This creates a new z with value twice x.
If we really want to access the external(global z) we can do 
so like this:

def f(x):
   global z
   z = 2*x
   return z

Now we modify the external z instead of creating a new one.
Generally this is a bad idea and you should pass the value 
into the function for modification:

def f(x,y):
   y = 2*x
   return y

But this still doesn't change the global z even if we pass 
it in as y, we have to assign the result:

z = f(5,z)

This now modifies global z asnd makes its value available 
to the function(as the parameter y).

HTH,

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld