473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing variables

kaarthikeyapreyan
107 New Member
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
Jul 3 '08 #1
6 1627
Laharl
849 Recognized Expert Contributor
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.
Jul 3 '08 #2
heiro
56 New Member
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()
  6.  
Jul 3 '08 #3
kaarthikeyapreyan
107 New Member
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
  3.  
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
Jul 4 '08 #4
Laharl
849 Recognized Expert Contributor
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
  7.  
Jul 6 '08 #5
AIProgrammer
28 New Member
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.
Jul 12 '08 #6
jlm699
314 Contributor
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.
Jul 14 '08 #7

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

Similar topics

2
by: Earl Teigrob | last post by:
I am programming ASP.NET using C#. I have been accessing static variables accross my entire application but now need to change some of the static variables that are session specific to instance...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
1
by: Nathan | last post by:
Hi, I have created a class library creating a number of forms and a few public variables. I have a project that references the .dll for this class library, and in that project I need to access...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
5
by: TS | last post by:
is it preferred to access member variables directly in code, on the page that declared them, versus going thru a property accessor? I would think that since theres no security concerns or anything...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.