473,322 Members | 1,345 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,322 software developers and data experts.

How Can I share common variable between two functions?

Can func1 share variable a with func2 without global keyword in python?

def func1():
a= 5
print(a)

def func2():
print(a)

func1()
func2()
Apr 26 '19 #1
2 1287
dwblas
626 Expert 512MB
For the code posted above, the variable, a, is local to/only exists in func1. You have to return it for it to exist outside the function. Every tutorial covers this. Tutorials are write once, read many, so we don't have to answer the same questions over and over and over. Start with this part of a the tutorials point tutorial http://www.tutorialspoint.com/python..._functions.htm
Apr 26 '19 #2
You can define both functions in a class, and make a as an attribute.

class A:
def __init__(self, a):
self.a = a
def func1(self):
self.a= 5
print(self.a)
def func2(self):
print(self.a)
obj = A(4)
obj.func2()
obj.func1()


The output will be.

4
5
Apr 27 '19 #3

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

Similar topics

5
by: Ikke | last post by:
I have to share a variable between browser sessions. One session for example changes the variable, the other sessions must see the changes (get the new value instead of the original value) it's...
3
by: elektrophyte | last post by:
I was looking at the PHP function "call_user_func()" (http://us3.php.net/call_user_func). It appears that this will solve a design problem in the app I'm working on. (I'm writing a class that will...
2
by: Rex_chaos | last post by:
I have define a MACRO varible as a common variable that be refered by several classes. #ifndef COMMON_VAR #define COMMON_VAR 1 .... #endif
2
by: MLH | last post by:
A97 allows separate procedures in a form module to share common label names. Access 2.0 would puke when that happened. For instance, if I had 2 procedures in an Access 2.0 form module with labels...
19
by: kelvSYC | last post by:
Can variable functions be implemented in C, and if so, how? Also, can you have a function or a function prototype as a member in a structure? -- I am only a mirage. -- comp.lang.c.moderated...
3
by: Garth17 | last post by:
I'm trying to figure out a solution for sharing common properties and methods in all me .aspx and .ascx pages. In classic ASP I would use include directives. So far I have made 2 base classes...
4
banning
by: banning | last post by:
i've been looking at javascript for a long time and just recently decided to buckle down and actually start learning it. I always see that whole var i .... why is i such a common variable name?
1
by: David24 | last post by:
i'm trying to share the variable: target This section of code: //draw rectangles for the edges filter if( coordinateList.CalculateEdges().Length > 0 ) { ...
7
by: viettrung | last post by:
Hi, I have two classes that share a common data list (specifically, a std::vector). This data list should be accessed by the two classes only, so I think using a global variable is not a good...
2
by: guddu | last post by:
Hi, I have to share a variable across two libraries. These libraries are build separately and share a common header file. I had tried this earlier using extern variable. That didn't help. Any...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.