Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > variable is local and global

Reply
Thread Tools

variable is local and global

 
 
Kyle Root
Guest
Posts: n/a
 
      07-26-2004
I'm writing a little program that compares two versions and tells you
whether you are upgrading, downgrading, or it's the same version.
Unfortunately, I haven't gotten very far, in fact I'm at a dead stop.

I need to make a variable in function available outside the function, so
I used "global", however now the program won't run and gives me the not
so friendly error:

bash-2.05b$ ./version.py
File "./version.py", line 11
def make_acceptable(version):
SyntaxError: name 'version' is local and global

-------------------------

I've looked and searched all over. Here's the code in case anyone can
spot the problem.

-------------------------
#!/usr/bin/python
import string
import sys
# This'll be replaced by an arg catching function, but for now this will
be fine
db_version="2.6.0"
pkg_version="2.6.6"
# We don't allow versions that have anything else besides digits and periods
acceptable_chars = "1234567890."
max_count = len(pkg_version)-1
def make_acceptable(version):
counter = 0
while 1 == 1:
one_char = version[(counter)counter + 1)]
if one_char in acceptable_chars:
if counter == max_count:
# Now we need to replace the periods with zeros.
version = version.replace(".","0")
global version
break
elif counter != max_count:
counter = counter + 1
elif one_char not in acceptable_chars:
print "EXITING: Versions may only contain digits and periods"
sys.exit()

make_acceptable(db_version)
db_version = version
print db_version
make_acceptable(pkg_version)
pkg_version = version
print pkg_version
-----------------------
Thanks:
Kyle
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      07-26-2004

Kyle> bash-2.05b$ ./version.py
Kyle> File "./version.py", line 11
Kyle> def make_acceptable(version):
Kyle> SyntaxError: name 'version' is local and global

Change either the name of the global variable or the name of the parameter.
They can't both be named "version".

Skip
 
Reply With Quote
 
 
 
 
Phil Frost
Guest
Posts: n/a
 
      07-26-2004
In the function make_acceptable, 'version' is both local and global
because it's a parameter, and you have used global to make it global.
Python does not allow it because what you mean by 'version' is
ambigious; it could mean the local, or the global. Rename either the
parameter or the global and it will work. Better yet, restructure your
code so that you don't need a global; either use the return value, or
make a class. Globals are never a good idea unless the cost of writing
it again is less than the cost of writing it right the first time.

On Mon, Jul 26, 2004 at 02:41:33AM +0000, Kyle Root wrote:
> I'm writing a little program that compares two versions and tells you
> whether you are upgrading, downgrading, or it's the same version.
> Unfortunately, I haven't gotten very far, in fact I'm at a dead stop.
>
> I need to make a variable in function available outside the function, so
> I used "global", however now the program won't run and gives me the not
> so friendly error:
>
> bash-2.05b$ ./version.py
> File "./version.py", line 11
> def make_acceptable(version):
> SyntaxError: name 'version' is local and global
>
> -------------------------
>
> I've looked and searched all over. Here's the code in case anyone can
> spot the problem.
>
> -------------------------
> #!/usr/bin/python
> import string
> import sys
> # This'll be replaced by an arg catching function, but for now this will
> be fine
> db_version="2.6.0"
> pkg_version="2.6.6"
> # We don't allow versions that have anything else besides digits and periods
> acceptable_chars = "1234567890."
> max_count = len(pkg_version)-1
> def make_acceptable(version):
> counter = 0
> while 1 == 1:
> one_char = version[(counter)counter + 1)]
> if one_char in acceptable_chars:
> if counter == max_count:
> # Now we need to replace the periods with
> zeros.
> version = version.replace(".","0")
> global version
> break
> elif counter != max_count:
> counter = counter + 1
> elif one_char not in acceptable_chars:
> print "EXITING: Versions may only contain digits and
> periods"
> sys.exit()
>
> make_acceptable(db_version)
> db_version = version
> print db_version
> make_acceptable(pkg_version)
> pkg_version = version
> print pkg_version
> -----------------------
> Thanks:
> Kyle

 
Reply With Quote
 
Kyle Root
Guest
Posts: n/a
 
      07-26-2004
thanks, I went the route of a return value.
works fine now.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM
scopes of local and global variable Pyenos Python 9 12-23-2006 07:43 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Accessing a global variable when there is a local variable in the same name Mohanasundaram C Programming 44 08-23-2004 11:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57