+ 16

[🐍PyTricks]: Functions are first-class citizens in Python

# Functions are first-class citizens in Python: # They can be passed as arguments to other functions, # returned as values from other functions, and # assigned to variables and stored in data structures. >>> def myfunc(a, b): ... return a + b ... >>> funcs = [myfunc] >>> funcs[0] <function myfunc at 0x107012230> >>> funcs[0](2, 3) 5

10th Mar 2018, 3:37 AM
Maninder $ingh
Maninder $ingh - avatar
8 Respostas
+ 21
Тимофей Антонов here is an example: def func1(x,y): print(x+y) def func2(x,y): print(x-y) def func3(x,y): print(x*y) myfuncs=[func1,func2,func3] a,b=2,3 for func in myfuncs: func(a,b)
19th May 2021, 6:06 PM
Abdulilah
Abdulilah - avatar
+ 8
In Javascript too, they are 1st class citizens. You should submit these as lessons from lesson factory instead of randomly posting as a post. https://www.sololearn.com/discuss/1082512/?ref=app
10th Mar 2018, 3:59 AM
Lord Krishna
Lord Krishna - avatar
+ 6
I've seen a lot of things in my life, but I've never seen functions being treated like that. Push it into an array! What are you allowing yourself, sir ?!
9th Apr 2021, 2:50 PM
Tima
+ 2
It's really helpful, thanks.
17th Jul 2022, 8:12 PM
Credo Tengey
+ 1
assigned to variables and stored in data structures. >>> def myfunc(a, b): ... return a + b ... >>> funcs = [myfunc] >>> funcs[0] <function myfunc at 0x107012230> >>> funcs[0](2, 3) 5
24th Jul 2021, 5:21 AM
Vaibhav Nehe
Vaibhav Nehe - avatar
0
def sums(x,y): return x+y def do_w(z,x,y): return z(z(x,y),z(x,y)) a=12 b=15 print (do_w(sums,a,b))
2nd Aug 2021, 2:04 AM
Dhananjay Pandey
Dhananjay Pandey - avatar
0
It usually helps for code reuse (less code)
20th Nov 2021, 10:20 PM
Khalid Alanazi
Khalid Alanazi - avatar
0
Тимофей Антонов thank you!! This makes it easy to understand
24th Dec 2021, 11:24 AM
Henry Jörgen