Assigning a function to a variable

前端 未结 5 967
灰色年华
灰色年华 2020-11-28 06:24

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 us

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 06:58

    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()
    

提交回复
热议问题