Sunday, 18 August 2013

python max function using 'key' and lambda expression

python max function using 'key' and lambda expression

I come from OOP background and trying to learn python. I am using the max
function which uses a lambda expression to return the instance of type
Player having maximum totalScore.
def winner():
w = max(players, key=lambda p: p.totalScore)
The function correctly returns instance of type Player having maximum
totalScore. I am confused about the following three things:
How does the max function work? What are the arguments it is taking? I
looked at the documentation but failed to understand.
What is use of the keyword key in max function? I know it is also used in
context of sort function
Meaning of the lambda expression? How to read them? How do they work?
These are all very noobish conceptual questions but will help me
understand the language. It would help if you could give examples to
explain. Thanks

No comments:

Post a Comment