Given self
argument you passed to your function, I assume you're dealing with classes. If that is the case, you could access that variable from self
object.
class Foo:
def __init__(self):
self.my_variable = "foo"
def my_function(self):
self.my_variable = "hello world"
def my_other_function(self):
if self.my_variable == "hello world":
print("pass")
my_class = Foo()
my_class.my_function()
my_class.my_other_function()
If you're not working with classes, the best way of doing this is returning your variable from your functions.
def my_function():
my_variable = "hello world"
return my_variable
def my_other_function():
my_var = myfunction()
if my_var == "hello world":
print("pass")
You may also work with global variables. To do that you should define a variable outside your functions and tell python to refer the that variable.
my_variable = "foo"
def my_function():
global my_variable
my_variable = "hello world"
def my_other_function():
global my_variable
if my_variable == "hello world":
print("pass")
Although it is very useful in scripting, it is not recommended to use big codes or applications.