How to call a variable in a function python

One approach would be to make oneFunction return the word so that you can use oneFunction instead of word in anotherFunction :

def oneFunction[lists]:
    category = random.choice[list[lists.keys[]]]
    return random.choice[lists[category]]

    
def anotherFunction[]:
    for letter in oneFunction[lists]:              
        print["_", end=" "]

Another approach is making anotherFunction accept word as a parameter which you can pass from the result of calling oneFunction:

def anotherFunction[words]:
    for letter in words:              
        print["_", end=" "]
anotherFunction[oneFunction[lists]]

And finally, you could define both of your functions in a class, and make word a member:

class Spam:
    def oneFunction[self, lists]:
        category=random.choice[list[lists.keys[]]]
        self.word=random.choice[lists[category]]

    def anotherFunction[self]:
        for letter in self.word:              
            print["_", end=" "]

Once you make a class, you have to instantiate an instance and access the member functions:

s = Spam[]
s.oneFunction[lists]
s.anotherFunction[]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we are going to see how to assign a function to a variable in Python. In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. 

    Implementation

    Simply assign a function to the desired variable but without [] i.e. just with the name of the function. If the variable is assigned with function along with the brackets [], None will be returned.

    Syntax:

    def func[]:
    {
    ..
    }
    
    var=func
    
    var[]
    var[]

    Example:

    Python3

    def a[]:

      print["GFG"]

    var=a

    var[]

    Output: 

    GFG

    The following programs will help you understand better:

    Example 1: 

    Python3

    x = 123

    def sum[]:

        x = 98

        print[x]

        print[globals[]['x']]

    print[x]

    z = sum

    z[]

    z[]

    Output:

    123
    98
    123
    98
    123

    Example 2: parameterized function

    Python3

    def even_num[a]:

        if a % 2 == 0:

            print["even number"]

        else:

            print["odd number"]

    z = even_num

    z[67]

    z[10]

    z[7]

    Output:

    odd number
    even number
    odd number

    Example 3:

    Python3

    def multiply_num[a]:

        b = 40

        r = a*b

        return r

    z = multiply_num

    print[z[6]]

    print[z[10]]

    print[z[100]]

    Output:

    240
    400
    4000

    How do you call a variable inside a function in Python?

    Use of “global†keyword to modify global variable inside a function. If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use 'global' keyword before the variable name at start of function i.e.

    Can you assign a function call to a variable in Python?

    In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without [] i.e. just with the name of the function.

    How do you use a variable from another function in Python?

    The variable can be assigned to the function object inside the function body. So the variable exists only after the function has been called. Once the function has been called, the variable will be associated with the function object.

    How do you access variables in Python?

    The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. Example – var_name. If you want to use that variable outside the method or class, you have to declared that variable as a global.

    Chủ Đề