Posts: 3
Threads: 1
Joined: Jun 2025
Reputation:
0
stackoverflow is gay so im posting here, what is the fucking lambda keyword in py? i've seen this shit in some codegolf emulators and crap but i don't know what it does. it looks like one of those ternaries in java but its not from what i can tell.
Posts: 3
Threads: 0
Joined: Apr 2025
Reputation:
1
it defines a basic anonymous function (called a lambda function), useful for when you ever need to return a function like this:
############################################
def createAdder():
return lambda a1, a2 : a1 + a2 # a1 and a2 are the arguments
adder = createAdder() # returns the lambda function
print( adder(5,5) ) # --> 10
############################################
it does look a little similar to the c ternary but is completely different
Posts: 3
Threads: 1
Joined: Jun 2025
Reputation:
0
thanks for the quick reply the syntax is a bit confusing though
is the [lamba arg] part where the args are defined? and after the '':'' is the actual function code?
Posts: 3
Threads: 0
Joined: Apr 2025
Reputation:
1
06-19-2025, 11:29 PM
(This post was last modified: 06-19-2025, 11:30 PM by queue.
Edit Reason: lamba -> lambda. i can spell trust!
)
yes, precisely! in this example here:
################################
example = lambda arg1, arg2 : print(arg1 + arg2)
example(25, 25) # --> 50
################################
"lamba arg1, arg2" are the arguments (essentially equiv. to def example(arg1, arg2)). after the ":" is where the code to the actual function goes. (in this case print(arg1 + arg2)).
Posts: 3
Threads: 1
Joined: Jun 2025
Reputation:
0
oh ok cool thanks for the quick response and help with the explanation