Can anyone tell me how to assign a variable to a function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone tell me how to assign a variable to a function?

hello() def hello(): print("Hello world!") In this case how to assign a variable to hello() function

26th Jun 2016, 4:37 PM
teambrj
4 Answers
+ 1
you want to assign the function to a variable and then invoke the function through the variable. right? If so, after assignment, invoke function using fn(). if not, pls elaborate your problem
26th Jun 2016, 5:21 PM
Anandaprakash S
+ 1
I'm not quite sure of what you meant exactly, but here's my thoughts. If you simply want to create a variable that contains the function you can for example: phello = hello() print (phello) However, if you meant to ask how to make a function that takes arguments: def hello (response=0): list = ["Hello World", "Hai there", "Greetings"] try: print(list[response]) except IndexError: print("List doesn't contain that response!") If no arguments are given: hello () # Same as hello (response=0) # Prints "Hello World" But with arguments, prints the specified line
26th Jun 2016, 8:53 PM
Lari Liuhamo
Lari Liuhamo - avatar
0
fn=hello
26th Jun 2016, 4:42 PM
Anandaprakash S
- 1
can u pl elaborate...it's not working..
26th Jun 2016, 4:55 PM
teambrj