473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to return a global variable from a function

5
Hello,

I'm trying to extract coordinates based on the name of imagery files. I created a script to do this using functions:

Expand|Select|Wrap|Line Numbers
  1. def lat(dd):
  2.     global lat
  3.     lat_lon = dd.split('_')[1]
  4.     if 'N' in lat_lon:
  5.         lat = int(lat_lon[1:3])
  6.     else:
  7.         lat = int(lat_lon[1:3]) * -1
  8.  
  9. def lon(dd):
  10.     global lon
  11.     lat_lon = dd.split('_')[1]
  12.     if 'W' in lat_lon:
  13.         lon = int(lat_lon[4:]) * -1
  14.     else:
  15.         lon = int(lat_lon[4:])
  16.  
  17. list = ['ASTMGTM2_N37W110', 'ASTMGTM2_N45E80']
  18. for n in list:
  19.     lat(n)
  20.     lon(n)
  21.     print lat, lon
The script runs correctly for the first value in the list. It returns '37 -110'. However, when it reaches the second value I receive an error:

TypeError: 'int' object is not callable

Does anyone know what I am doing wrong? I am new to python and have never really used the 'global' variable before.

I also tried using the 'return' keyword instead of the global variable:
Expand|Select|Wrap|Line Numbers
  1. def lat(dd):
  2.     lat_lon = dd.split('_')[1]
  3.     if 'N' in lat_lon:
  4.         lat = int(lat_lon[1:3])
  5.         return lat
  6.     else:
  7.         lat = int(lat_lon[1:3]) * -1
  8.         return lat
  9.  
  10. def lon(dd):
  11.     lat_lon = dd.split('_')[1]
  12.     if 'W' in lat_lon:
  13.         lon = int(lat_lon[4:]) * -1
  14.         return lon
  15.     else:
  16.         lon = int(lat_lon[4:])
  17.         return lon
  18.  
  19. list = ['ASTMGTM2_N37W110', 'ASTMGTM2_N45E80']
  20. for n in list:
  21.     lat(n)
  22.     lon(n)
  23.     print lat, lon
This does not error, but I cannot get the numeric values. I receive:

<function lat at 0x03681F30> <function lon at 0x075A9530>

I am currently using Python 2.6 and Windows 7 64-bit. Thank you for your help!
Nov 16 '11 #1

✓ answered by bvdet

Use unique identifiers (names) for your functions and values. In function lat() you can eliminate the assignment to identifier lat.
Expand|Select|Wrap|Line Numbers
  1. def lat(dd):
  2.     lat_lon = dd.split('_')[1]
  3.     if 'N' in lat_lon:
  4.         return int(lat_lon[1:3])
  5.     else:
  6.         return int(lat_lon[1:3]) * -1
Don't use list for a variable name. Built-in Python function list() will be masked.

Your values can be accessed like this:
Expand|Select|Wrap|Line Numbers
  1. seq = ['ASTMGTM2_N37W110', 'ASTMGTM2_N45E80']
  2. for n in seq:
  3.     v1 = lat(n)
  4.     v2 = lon(n)
  5.     print v1, v2

2 2216
bvdet
2,851 Expert Mod 2GB
Use unique identifiers (names) for your functions and values. In function lat() you can eliminate the assignment to identifier lat.
Expand|Select|Wrap|Line Numbers
  1. def lat(dd):
  2.     lat_lon = dd.split('_')[1]
  3.     if 'N' in lat_lon:
  4.         return int(lat_lon[1:3])
  5.     else:
  6.         return int(lat_lon[1:3]) * -1
Don't use list for a variable name. Built-in Python function list() will be masked.

Your values can be accessed like this:
Expand|Select|Wrap|Line Numbers
  1. seq = ['ASTMGTM2_N37W110', 'ASTMGTM2_N45E80']
  2. for n in seq:
  3.     v1 = lat(n)
  4.     v2 = lon(n)
  5.     print v1, v2
Nov 16 '11 #2
Jake S
5
Thanks, bvdet! That worked like a charm.
Nov 17 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
7
by: johkar | last post by:
This is a bit tricky to describe, but here goes. Any help appreciated. John I declare multiple variables halfway down the page. The number between "mech" and "Num" is generated dynamically in...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
19
by: moxm | last post by:
I have a statement declares a globle variable like this : char *pname = NULL; Then I used splint to check the code, I got errors: err.c:8:15: Global pname initialized to null value: pname =...
6
by: Eddie | last post by:
I found it's not possible. But if I have a function which is called by every forms, do I have to copy the function to every form class? It looks like non-sense. Is there any other options?
2
by: mars | last post by:
In my application, global variable A, during its construction, depends on another global variable, say B. However, variable B may have not been initialized by then. How can I make sure that...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
2
by: Kurda Yon | last post by:
Hi, I would like to declare a global variable, which is seen to a particular function. If I do as the following it works: x = 1 def test(): global x print x return 1
4
by: Mister Richards | last post by:
I am trying to create a global variable in C++. I am fairly new to C++ and keep getting an error message my getDoubleNumbr function does not recognize the number variable. I know that the error is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.