Assigning a function to a variable

Question:

Let’s say I have a function

def x():
    print(20)

Now I want to assign the function to a variable called y, so that if I use the y it calls the function x again. if i simply do the assignment y = x(), it returns None.

Asked By: stensootla

||

Answers:

You simply don’t call the function.

>>> def x():
>>>     print(20)
>>> y = x
>>> y()
20

The brackets tell Python that you are calling the function, so when you put them there, it calls the function and assigns y the value returned by x (which in this case is None).

Answered By: Gareth Latty

When you assign a function to a variable you don’t use the () but simply the name of the function.

In your case given def x(): ..., and variable silly_var you would do something like this:

silly_var = x

and then you can call the function either with

x()

or

silly_var()
Answered By: Levon

when you perform y=x() you are actually assigning y to the result of calling the function object x and the function has a return value of None. Function calls in python are performed using (). To assign x to y so you can call y just like you would x you assign the function object x to y like y=x and call the function using y()

Answered By: cobie

lambda should be useful for this case.
For example,

  1. create function y=x+1

    y=lambda x:x+1

  2. call the function

    y(1)

    then return 2.

Answered By: Pengju Zhao

The syntax

def x():
    print(20)

is basically the same as x = lambda: print(20) (there are some differences under the hood, but for most pratical purposes, the results the same).

The syntax

def y(t):
   return t**2

is basically the same as y= lambda t: t**2. When you define a function, you’re creating a variable that has the function as its value. In the first example, you’re setting x to be the function lambda: print(20). So x now refers to that function. x() is not the function, it’s the call of the function. In python, functions are simply a type of variable, and can generally be used like any other variable. For example:

def power_function(power):
      return  lambda x : x**power
power_function(3)(2)

This returns 8. power_function is a function that returns a function as output. When it’s called on 3, it returns a function that cubes the input, so when that function is called on the input 2, it returns 8. You could do cube = power_function(3), and now cube(2) would return 8.

Answered By: Acccumulation

I don’t know what is the value/usefulness of renaming a function and call it with the new name. But using a string as function name, e.g. obtained from the command line, has some value/usefulness:

import sys
fun = eval(sys.argv[1])
fun()

In the present case, fun = x.

Answered By: Apostolos
def x():
   print(20)
   return 10

y = x
y()
print(y)

gives the output

20
<function x at 0x7fc548cd3040>

so it does not actually assign the value returned by x() to the variable y.

Answered By: muratozyurt
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.