non-implicit globals -- missing feature

Curly Joe woooee at yahoo.com
Mon Jun 16 16:51:07 EDT 2003


In the first case, i.e. using mysub(), the x
variable in the function is local to the function.  If
you call mysub() and then print x, x will still be
equal to 1.  In the second case, you are passing x to 
 the function.  First, we would have to assume that
 you are passing the global variable because you
expressly want to use it.  In any case this would also
leave the global x=1 because you don't return the new 
 variable (at least on my machine).  If you want to
 change the value, then you would have to return the
new value, which would mean that your intent was to
change it.  If this is a problem, consider using a 
 class, in which case the global variables prepend a
 "self" (global to the class this is).  Try running
this code (not tested).

<CODE><CODE>
x=1
def funct1() :
    x=7

def funct2(x) :
    x=8

def funct3(x) :
    x=9
    return x

if __name__ == "__main__" :
    print "orig value for x = ", x
    funct1()
    print "value after funct1 = ", x
    funct2(x)
    print "value after funct2 = ", x
    x = funct3(x)
    print "value after funct3 = ", x

If this doesn't answer your question, then please
elaborate.
> 
> --- In python-list at yahoogroups.com, Helmut Jarausch
> <jarausch at s...> 
> wrote:
> > Hi,
> > 
> > I'm missing a feature (in many programming
> languages including
> > Python) that does not use global variables unless
> requested.
> > 
> > Is there any means in Python to catch errors like
> this following 
> one?
> > 
> > x=1
> > 
> > # many many lines here
> > 
> > def mysub()
> >    # x=7   #forgot this assignment
> >    # ....
> >    anothersub(x)  # this uses the global x
> unfortunately
> >                   # how can I get an exception
> here ?
> > 
> > So I would prefer if there were an option / pragma
> > which say  # __no_implicit_globals__
> > 
> > Is there any way to get this behaviour?
> > 
> > Thanks for a comment
> > 
> > Helmut Jarausch
> > 
> > Lehrstuhl fuer Numerische Mathematik
> > RWTH - Aachen University
> > D 52056 Aachen, Germany
> > 
> > -- 
> >
> http://mail.python.org/mailman/listinfo/python-list
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com





More information about the Python-list mailing list