How to access variables defined and declared in one function in another function?
The C++ way is to pass abc
by reference to your function:
void function1()
{
std::string abc;
function2(abc);
}
void function2(std::string &passed)
{
passed = "new string";
}
You may also pass your string as a pointer and dereference it in function2. This is more the C-style way of doing things and is not as safe (e.g. a NULL pointer could be passed in, and without good error checking it will cause undefined behavior or crashes.
void function1()
{
std::string abc;
function2(&abc);
}
void function2(std::string *passed)
{
*passed = "new string";
}
Calling variable defined inside one function from another function
One approach would be to make oneFunction
return the word so that you can use oneFunction
instead of word
in anotherFunction
:
def oneFunction(lists):
category = random.choice(list(lists.keys()))
return random.choice(lists[category])
def anotherFunction():
for letter in oneFunction(lists):
print("_", end=" ")
Another approach is making anotherFunction
accept word
as a parameter which you can pass from the result of calling oneFunction
:
def anotherFunction(words):
for letter in words:
print("_", end=" ")
anotherFunction(oneFunction(lists))
And finally, you could define both of your functions in a class, and make word
a member:
class Spam:
def oneFunction(self, lists):
category=random.choice(list(lists.keys()))
self.word=random.choice(lists[category])
def anotherFunction(self):
for letter in self.word:
print("_", end=" ")
Once you make a class, you have to instantiate an instance and access the member functions:
s = Spam()
s.oneFunction(lists)
s.anotherFunction()
How to access a variable in one python function in another function
You have to declare the variable to be global, then use it. Like so:
def add_to_outside():
global outside #say that it is global
outside = 1 #now create it!
def see_it():
global outside #say that it is global
print(outside)
##As shown:
add_to_outside()
see_it()
#output: 1
The keyword global
at the start makes all variables of that name in the function reference the global values. You don't say a variable is global and change it in the same statement.
Also, only put the global
keyword at the start of the function. It doesn’t need to be next to the changes to the variables, and is only needed once.
To declare multiple variables global, do it like this:
global var1, var2, var3 #etc.
Access variable from function to another function
In object oriented programming, different methods (functions) of an object can share common variables using the 'this' keyword. Here in this example I declare a class called 'Context' with two methods 'nameOutside' and 'nameInside'. Then I instantiate an object from the class, storing the reference in the 'context' variable. Finally I call the nameInside() method of that object.
class Context {
nameOutside() {
console.log(this.name);
}
nameInside() {
this.name = "my name";
this.nameOutside();
}
}
let context = new Context();
context.nameInside();
How to Access Function variables in Another Function
Return them from your first function and accept them in your second function. Example -
def xxx():
a=10
b=15
c=20
return a,b
def yyy():
a,b = xxx()
print a ### value a from xxx()
print b ### value b from xxx()
yyy()
Access variable from a different function from another file
You can access the value of a variable in a function by 1) returning the value in the function, 2) use a global variable in the module, or 3) define a class.
If only want to access a single variable local to a function then the function should return that value. A benefit of the class definition is that you may define as many variables as you need to access.
1. Return value
def champs_info(champname:str, tier_str:int):
...
tier = tier_access.text
return tier
2. global
tier = None
def champs_info(champname:str, tier_str:int):
global tier
...
tier = tier_access.text
Global tier vairable is accessed.
from mypythonlib import myfunctions
def test_champs_info():
myfunctions.champs_info("abomination", 6)
return myfunctions.tier
print(test_champs_info())
3. class definition:
class Champ:
def __init__(self):
self.tier = None
def champs_info(self, champname:str, tier_str:int):
...
self.tier = tier_access.text
test_functions.py can call champs_info() in this manner.
from mypythonlib import myfunctions
def test_champs_info():
info = myfunctions.Champ()
info.champs_info("abomination", 6)
return info.tier
print(test_champs_info())
Related Topics
Why Use !! When Converting Int to Bool
How to Enable_Shared_From_This of Both Parent and Derived
Should I Use Public or Private Variables
Qt - Remove All Widgets from Layout
Template Function Specialization for Integer Types
Why Does the C++ Standard Algorithm "Count" Return a Difference_Type Instead of Size_T
What Is the Safe Way to Fill Multidimensional Array Using Std::Fill
Passing a Pointer to a Class Member Function as a Parameter
Is C++ Static Member Variable Initialization Thread-Safe
Int *Array = New Int[N]; What Is This Function Actually Doing
How Can This Structure Have Sizeof == 0
Returning Non-Const Reference from a Const Member Function
What's Preferred Pattern for Reading Lines from a File in C++
Erasing While Iterating an Std::List
Can You Have a Triple Minus Signs in C Programming? What Does It Mean
Std::Remove_Const with Const References
What Is Better: Reserve Vector Capacity, Preallocate to Size or Push Back in Loop