Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what the fuck is a lambda
 
#1
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.
Reply
#2
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
Reply
#3
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?
Reply
#4
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)).
Reply
#5
oh ok cool thanks for the quick response and help with the explanation
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)