Last updated: Apr 11, 2024
Reading timeยท4 min
There are multiple ways that enable you to use a variable from another function in Python:
self
object.global
keyword to declare the variable as global.Let's look at an example of returning the variable from the first function.
def func_a(): odd_numbers = [1, 3, 5, 7, 9] return odd_numbers def func_b(): a_list = func_a() print(a_list) func_b() # ๐๏ธ [1, 3, 5, 7, 9]
The first function defines a variable and returns it using the return
statement.
The second function invokes the first function using parentheses and stores its return value in a new variable.
If the functions take arguments, make sure to supply them as well.
def func_a(num): odd_numbers = [1, 3, 5, 7, 9, num] return odd_numbers def func_b(): a_list = func_a(11) print(a_list) func_b() # ๐๏ธ [1, 3, 5, 7, 9, 11]
The first function takes a number as an argument, so we had to pass it when calling it.
Alternatively, you can pass the variable as an argument to the second function.
def func_a(num): odd_numbers = [1, 3, 5, 7, 9, num] return odd_numbers def func_b(a_list): print(a_list) func_b(func_a(15)) # ๐๏ธ [1, 3, 5, 7, 9, 15]
The first function returns a list of numbers using the return
statement.
The second function takes a list as a parameter and prints it.
When calling the second function, we passed it the result of calling the first function.
The second function gets called with the list that is returned from the second function.
Alternatively, you can wrap the functions in a class.
class Employee(): def __init__(self, first, last): self.first = first self.last = last self.salary = 100 def increase_salary(self): self.salary += 100 print(self.salary) def decrease_salary(self): self.salary -= 100 print(self.salary) emp1 = Employee('bobby', 'hadz') emp1.increase_salary() # ๐๏ธ 200 emp1.increase_salary() # ๐๏ธ 300 emp1.decrease_salary() # ๐๏ธ 200 emp1.decrease_salary() # ๐๏ธ 100
The Employee()
class takes 2 parameters upon instantiation - first
and
last
.
The increase_salary
and decrease_salary
methods don't take any parameters
and can be called on a class instance.
The methods increase or decrease the salary
instance variable.
You can optionally set a default value for the variable in the __init__
method
of the class.
The __init__() method is automatically run when the class is instantiated.
You can also mark a variable as global to be able to use it in another function.
full_name = '' def function_a(): global full_name full_name = 'bobby hadz' def function_b(): print(full_name) function_a() function_b() # ๐๏ธ bobby hadz
We initialized the variable in the global scope, setting it to an empty string.
The global
keyword enables us to update the value of the variable from the
outer scope from within a function.
Once the variable's value has been updated, we can access the updated value in the other function.
Notice that we first called function_a
to update the value of the global
variable.
Had we called function_b
first, the value of the variable would have been an
empty string.
You can also use a variable from another function by setting function attributes.
def function_a(): function_a.website = 'bobbyhadz.com' print(function_a.website) def function_b(): print(function_a.website) function_a() # ๐๏ธ bobbyhadz.com function_b() # ๐๏ธ bobby hadz
We used dot notation to set the website
attribute in function_a
.
You can access the attribute directly on the function object, e.g.
function_name.attribute_name
.
We are also able to access the attribute from other functions.
However, note that function_a
has to be invoked first as the function sets the
attribute upon invocation.
If you try to call function_b
first, you will get an AttributeError
exception because the attribute has not been set yet.
You can learn more about the related topics by checking out the following tutorials: