On 07/31/2014 08:22 PM, Ben Finney wrote:
memilanuk <memila...@gmail.com> writes:
>> So... the similarity between dict.get() and dict.get as used here is
>> kinda confusing me.
>
> I hope that helps. They are the same function; but the former is
> *calling* the function object, and optionally using the return value;
> the latter is referring to the function object *as* a value.
Conceptually, that makes sense. Practically... having a little trouble
getting my head around it - see below.
max_key = max(counts, key=counts.get)
This specifies ‘counts.get’, without calling it. The expression
‘counts.get’ evaluates to that function object.
That value is then used as the value for the ‘key’ parameter when
calling ‘max’ here.
Been reading a bit more in the mean time, trying to grok that 'key'
parameter for max()... and of course the python docs for max(iterable,
key=) refer to the docs for list.sort() ;)
Kind of diverging off the original question a bit... but since it did
include the max() code in it... I'm having a bit of difficulty with the
whole 'key=' parameter when combined with counts.get here.
So counts is the iterable, and counts.get is the key used to iterate
through it?
I guess I'm not getting how the whole key, function object bit works
here in actual practice.
if we have
counts = {'a':1, 'b':22, 'c':100}
then counts.get('b') should return 22. I got that much.
And counts.get is just an uncalled version of that:
foo = counts.get
foo('b')
should return 22 as well. Think I got that as well.
Where things are going pear-shaped is how counts.get can function as a
'key' when we don't actually supply () (or anything inside them) to
specify what k,v pair we want, and how that relates back to the iterable
for max(), counts?
Monte
--
Shiny! Let's be bad guys.
Reach me @ memilanuk (at) gmail dot com
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor