question on global variables

andy surany mongo57a at comcast.net
Thu Oct 10 02:21:47 EDT 2002


Thanks Mark. Not to worry, I'm not retching.....

There is data that I am returning - but this particular item happens to be
the number of records returned from a query (the data set is returned). And
I am using it everywhere. It is set in the query function - which is called
once. I tried using different calling and return options, and finally came
to the conclusion that what I really did want was the gloabal variable.

Regards,

Andy
-----Original Message-----
From: Mark McEahern <marklists at mceahern.com>
To: mongo57a at comcast.net <mongo57a at comcast.net>; python-list at python.org
<python-list at python.org>
Date: Thursday, October 10, 2002 12:44 AM
Subject: RE: question on global variables


>> Somewhat new to Python here.....
>>
>> I want to change/use a value throughout my program - what I would
normally
>> refer to as a "global" variable.
>>
>> I see that there is a "global" command - and I assume its use would be
>> global var_name. This I have coded (works) but I am unable to use
>> it "global name var_name is not defined".
>
>Consider the following example.  It should make you retch:
>
>***
>
>#!/usr/bin/env python
>
>global i
>i = 1
>
>def foo():
>    global i
>    i += 1
>
>def bar():
>    global i
>    i += 1
>
>foo()
>bar()
>
>print i
>
>***
>
>Why should it make you retch?  Because there's no reason whatsoever for me
>to use global--foo and bar would be much clearer if they operated on input
>and returned as output whatever they need to communicate to their callers:
>
>***
>
>#!/usr/bin/env python
>
>def foo(i):
>    return i + 1
>
>def bar(i):
>    return i + 1
>
>i = 1
>i = foo(i)
>i = bar(i)
>
>print i
>
>***
>
>In other words--why let the fact that Python has a global statement fool
you
>into thinking you've found a problem for which it's the only much less the
>best hammer?
>
>Cheers,
>
>// mark
>
>-
>





More information about the Python-list mailing list