Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Accessing variables

Question posted by: kaarthikeyapreyan (Member) on July 3rd, 2008 06:25 AM
Is there any possibility that i could access a the variables that i have defined in a function
for example
Expand|Select|Wrap|Line Numbers
  1. def foo():
  2.   i = 10
  3.   i = i + 10


I would like to retrieve the value and use it in another function
I was able to find a way accessing the variables declared in methods
my problem is when i do it with functions and
how does python work with the scope of the variables
Laharl's Avatar
Laharl
Expert
795 Posts
July 3rd, 2008
12:42 PM
#2

Re: Accessing variables
You can return variables like that with the 'return' keyword, which also ends the function when it's returned. To return multiple values at once, you need to use a list/tuple. You'll need to assign the output of the function to another variable.

Reply
heiro's Avatar
heiro
Member
53 Posts
July 3rd, 2008
06:18 PM
#3

Re: Accessing variables
Here is the example.
Expand|Select|Wrap|Line Numbers
  1. def foo():
  2.      i = 10
  3.      i = i + 10
  4.      return i
  5. print foo()

Reply
kaarthikeyapreyan's Avatar
kaarthikeyapreyan
Member
73 Posts
July 4th, 2008
04:58 AM
#4

Re: Accessing variables
Sorry about not being so clear
my actual question goes this way

I define two functions

Expand|Select|Wrap|Line Numbers
  1. def foo():
  2.   i=10


Expand|Select|Wrap|Line Numbers
  1. def bar():
  2.   sum = 10 + i # the i should be from the function foo


i would not prefer to use the return function cause i am returning something else with it
to be more precise this is how i do it for a method variable

Expand|Select|Wrap|Line Numbers
  1. >>> class a:
  2. ...   def foo(self):
  3. ...     self.i = 10
  4. ...     self.i = self.i + 10
  5. ... 
  6. >>> obj = a()
  7. >>> obj.foo()
  8. >>> obj.i
  9. 20


i could manipulate the value of i using the class instance
similarly how do i do it for variables inside a function

Reply
Laharl's Avatar
Laharl
Expert
795 Posts
July 6th, 2008
12:51 AM
#5

Re: Accessing variables
The easiest way to do this is to declare i in global scope and then use the global keyword to bring it in. This does bring up some scoping issues I'm not entirely familiar with...

Expand|Select|Wrap|Line Numbers
  1. def foo():
  2.     global i
  3.     i += 10
  4. i = 0
  5. foo()
  6. print i #prints 10

Reply
AIProgrammer's Avatar
AIProgrammer
Newbie
23 Posts
July 12th, 2008
10:51 AM
#6

Re: Accessing variables
Hi,
Your problem has two solutions:
SOULTION1:
put all tha values in a tuple and return it.
CODE:
def fn():
i =10
j = i+10
k = (i,j)
return k
l = fn()
print l
OUTPUT:
(10,20)

Then separate the elements simply as if for any other tuple.

SOULTION2:
declare variables you want out of the function as global.

CODE:
def fn():
global i
i =10
j = i+10
return j
k = fn()
print i
print k

Here, i is global, that is, as if it is 'defined' outside of any fn in the module. It can be accessed from any fn in same module (and also on different module using

CODE:
from module import *

Think that should solve your problem.

Bye.

Reply
jlm699's Avatar
jlm699
Needs Regular Fix
313 Posts
July 14th, 2008
12:19 PM
#7

Re: Accessing variables
Quote:
Hi,
Your problem has two solutions...
[redacted]

Please use code tags in your posts. Refer to the Posting Guidelines about how to properly respond to questions.

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,873 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Python Forum Contributors