Global Variables
Variables that are created outside of a function [as in all of the examples above] are known as global variables.
Global variables can be used by everyone, both inside of functions and outside.
Example
Create a variable outside of a function, and use it inside the function
x = "awesome"
def myfunc[]:
print["Python is " + x]
myfunc[]
Try it Yourself »
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
Example
Create a variable inside a function, with the same name as the global variable
x = "awesome"
def myfunc[]:
x = "fantastic"
print["Python is " + x]
myfunc[]
print["Python is " + x]
Try it Yourself »
The global Keyword
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.
To create a global variable inside a function, you can use
the global
keyword.
Example
If you use the global
keyword, the variable belongs to the global scope:
def myfunc[]:
global x
x = "fantastic"
myfunc[]
print["Python is " + x]
Try it Yourself »
Also, use the global
keyword if you want to change a global variable inside a function.
Example
To change the value of a global variable inside a function, refer to the variable by using the global
keyword:
x = "awesome"
def myfunc[]:
global x
x = "fantastic"
myfunc[]
print["Python is " + x]
Try it Yourself »
If you want to avoid global
, one possible approach is to define a class. Each class instance has its own attributes; there is also a class attribute space where instances can share an attribute between them.
Object-oriented programming can be challenging to get into if you are new to Python, but this might actually be a good time to start playing with it.
class Thing:
shared = "foo"
def __init__[self]:
"""
This gets called when you create a new Thing[]
"""
self.bar = "baz" # default value for new instances
def get_bar[self]:
return self.bar
def set_bar[self, value]:
self.bar = value
Now, let's create two instances.
first = Thing[]
second = Thing[]
The get_bar
and set_bar
methods are not strictly
necessary in simple examples like this one. You can also do
second.bar = "ick"
print[second.bar]
# "ick"
print[first.bar]
# "baz"
[though for more complex scenarios, you probably want to require users to call the setter and getter methods; there are ways to force this - see e.g. What's the pythonic way to use getters and setters?]
If you change a class attribute via one instance, it will not be changed in the other instances, either.
second.shared = "poo"
print[first.shared]
# "foo"
But if you change it in the class itself, it will be changed in all the instances which have not separately overridden the shared value.
Thing.shared = "zoom"
print[first.shared]
# "zoom"
print[second.shared]
# "poo", still
To recap, you create a new Thing
instance by calling Thing[]
; this will run the __init__
method before returning the new instance. Inside the class, the instance is the first argument to every [non-static, non-class] method, and conventionally called self
[though you could get away with calling it shirley
if
you wanted to, as far as the Python interpreter is concerned].
There's a lot more to classes; the main selling point is probably that you can create subclasses which inherit from their parent class but can override some behaviors [common examples often involve real-world concepts like animals or vehicles, but a class can just be anything where you want to create a type and encapsulate its behavior, and perhaps override some methods in derived types].
Table of Contents
- Get Variable from Function in Python
- Using the return statement to get variable from function in Python
- Using attributes to get variable from function in Python
- Conclusion
A function is a reusable piece of code that can be called at different places in a program. A function can execute some statements when called and return some value if desired. Any variable declared within the function cannot be accessed from the outside.
We will discuss how to get variable from function in Python.
Using the return
statement to get variable from function in Python
The return
statement in Python is used to return some value from a function in Python. Since we cannot access the variable defined within the function from the
outside, we can return it using the return
statement to get variable from function in Python.
For example,
def fun[]: a=15 returna n=fun[] print[n] |
Output:
15
We can also return multiple variables. For this, we can use objects like lists, dictionaries, and more. We will store the variables in one such object and return it from the function.
See the code below.
def fun[]: d={'a':15,'b':10} returnd n= fun[] print[n['a']] |
Output:
15
In the above example, we return a dictionary from the function. We access the variable values using the variable’s key from the dictionary and print it.
Using attributes to get variable from function in Python
Everything is an object in Python, even functions. We can assign functions with some arbitrary values that can work as attributes. These values can be accessed from outside a function. This way, we can get variable from function in Python.
See the code below.
def fun[]: fun.a=15 fun[] n=fun.a print[n] |
Output:
15
In the above example, we assign an attribute value to the function. We first call this function and then we can use this variable using the function name.
Conclusion
In this tutorial, we discussed how to get variable from function in Python. There is no direct way to access a variable declared within the function from outside. So, to get variable from function in Python, we can directly return the value. We discussed how to return single and multiple variables. Another way to get variable from function in Python is by assigning them as attributes of the function. This way, we can access the attribute values after calling the function.