This is a test version of Biostars. For the public version, visit https://www.biostars.org.
accessing variable value outside the function

I am working with certain codes in python and I wanted to know if a certain value of a variable can be accesssed outside the function without explicitly declaring it a global variable. for example:

I was using this:

def func1():
    v1 = "value1"
    return v1
    print ("This is the first function")

def func2():
    v2 = "value2"
    func1()
    print ("This is the second function")

fo = input ("Enter 1/2\n")
if fo == '1':
    func1()
elif fo == '2':
    func2()
else:
    print ("invalid")
print (v1)

It results me this error:

    print (v1)
NameError: name 'v1' is not defined

Can I use this value of 'v1' variable outside the function without defining it to b 'global'??

python

Hello mdsiddra!

We believe that this post does not fit the main topic of this site.

This is a pure programming question. Please search (do not ask) StackOverflow

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

2 answers

You won't find many programming languages where all variables have global scope. If you want to access v1 outside of func1 then use v = func() in the main function and ensure func1 returns v1.

Are you saying that I should access the value by making an object of the function? what if the function is using more than one values then how do I access a particular value? Can you elaborate?

If I wanted to suggest that you should make an object out of the function then I would have written that. In all programming languages that you will likely ever use, you can never access variables inside random functions. If you want to have access to those variables then you must return them to the calling function (and further up). You can get around that by using objects, but that's similarish to using global variables (they're then neither global nor local, but somewhere in between).

Well okay. Thankyou for your response.. :)

Technically speaking, in python, a function is an object regardless.

If the function utilises many variables that you want access to, you return as much or as little as you need. One of the neater aspects in python is unpacking, so you can do this easily, e.g.:

def function1(value1, value2):
    v1 = value1
    v2 = value2
    return v1, v2

 a, b = function1(value1, value2)

If you had lots of info to return, you might be better off internally collecting all the variables in to an array and returning that.

If you code becomes this object-oriented, with lots of 'reaching in to' objects to access variables etc, you might want to consider making a Class which can have variables and functions associated with it. You can instantiate multiple classes if they're going to contain more than one 'version' of some data.

You have two options (actually more but lets keep it with two)

As Devon Ryan suggest and also my preference:

def func1():
    v1 = "value1"
    print ("This is the first function")
    return v1

def func2():
    v2 = "value2"
    func1()
    print ("This is the second function")

fo = input ("Enter 1/2\n")
if fo == '1':
    funcValue = func1()
elif fo == '2':
    funcValue = func2()
else:
    print ("invalid")

print (funcValue)

Also you need to print first before returning something. After the return the function stops.

Or make a global variable, but don't do this if it is not necessary:

v1 = ""
v2 = ""
def func1():
    global v1
    v1 = "value1"
    print ("This is the first function")
    return v1

def func2():
    global v2
    v2 = "value2"
    func1()
    print ("This is the second function")

fo = input ("Enter 1/2\n")
if fo == '1':
    func1()
elif fo == '2':
    func2()
else:
    print ("invalid")

print (v1)

Alright, I have used the global declaration method already but I wanted to use without using global declaration. Thankyou for your suggestions. I am gonna try the method if it helps me. Thankyou..!

Let us know if you have problems

Log in to answer this question.