Get Variable from Function in Python

Get Variable from Function in Python

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,

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.

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.

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.