How to access variables present in different functions?

Asked

Viewed 5,642 times

3

What is the best way to access a variable that was created in a function? For example:

def Luggage():
    mala = []
    take = raw_input("O que você deseja levar na sua viagem?")
    mala.append(take)

In case I wanted to access and use the list mala in another function, how should I proceed? In that case, it would be more interesting to use functions created within classes?

  • Did the answer solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.

2 answers

7

Everything you put inside a function is local to this function, you can not use elsewhere. There are some solutions.

The first is to return the value - but not the variable - you want, and then whoever calls the function can "capture" that value and do what you want.

Or you must create a variable in another location that can be used within the function. In this case there can be two alternatives. Some types of variables may have their value passed to the function as argument and the function updates the received parameter, when the function returns, the variable that had this value before the call will have the value placed inside the function.

The most common in these cases is to create a class that contains variables that are present in the class, then all functions within the class will have access to these class variables, so any change in it within a function will reflect to all other.

For very simple examples, and only in these cases, it is possible to declare the variable outside the function and it will be accessed by all functions. This is the equivalent of what you do with classes. But with classes you have a scope defined and does not cause confusion. In a very simple code that will not be used in a complex system, ie in a code of script even, you can use without any confusion, but in other situations you should not use this form.

These two concepts of using variables outside the function are more advanced and I think we should not try now that it still does not dominate the basic functioning of functions. For now just put a return:

def Luggage():
    mala = []
    take = raw_input("O que você deseja levar na sua viagem?")
    mala.append(take)
    return mala

Calling:

x = Luggage()
print(x)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I understand. But returning the value, can I access it within another function? For example: def Hotel(): if shampoo in mala: print "Tomar banho"

  • You can pass it as a parameter to this function. Data needs to be communicated between functions. Unless you use the class. But don’t think creating a class is the answer to everything. Before considering creating a class, understand everything about this, most people, especially when starting abuse classes to solve a problem that should be solved differently. The print() is a function is the data was used inside it. You did not see, but it was used because it was passed as parameter.

  • @bigown he could define the variable as global, for example global mala?

  • So you could access it anywhere in the code

  • 1

    Yes, but I will not teach this gambit :D Okay, I will put, with caution :)

  • tabem could use a decorator with the function Luggage

Show 1 more comment

1

Suggestion is to use global variable definition:

def Luggage():
    global mala
    mala = []
    take = "O que voce deseja levar na sua viagem?"
    mala.append(take)
    take = "----teste------"
    mala.append(take)
    return mala

Luggage()

for i in mala:
    print (i)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.