Re: [Tutor] Python functions are first-class citizens

2014-08-01 Thread Alan Gauld
On 01/08/14 09:33, Ben Finney wrote: I'm being a stickler on this point because “iterate” implies something quite specific in Python, and this behaviour is not implied by the purpose of ‘max’. Instead, think only “finds the largest item in the collection”. Nope, think the largest in an iterat

Re: [Tutor] Python functions are first-class citizens

2014-08-01 Thread Steven D'Aprano
On Thu, Jul 31, 2014 at 10:12:49PM -0700, memilanuk wrote: > 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

Re: [Tutor] Python functions are first-class citizens

2014-08-01 Thread Steven D'Aprano
On Fri, Aug 01, 2014 at 02:37:43AM -0700, memilanuk wrote: > On 08/01/2014 01:33 AM, Ben Finney wrote: > >* There is no guarantee all the items will be seen. ‘max’ could very > > well decide it's got the largest item and return it at any point. It's > > an implementation detail how it does tha

Re: [Tutor] Python functions are first-class citizens

2014-08-01 Thread memilanuk
On 08/01/2014 01:33 AM, Ben Finney wrote: Ah! Now I get a better idea why you're confused. There are two distinct uses of “key” going on. Ahhh... no. Yes, I realize there were two different uses of 'key' and no, I didn't think they were necessarily the same. * There is no guarantee all t

Re: [Tutor] Python functions are first-class citizens

2014-08-01 Thread Ben Finney
memilanuk writes: > On 07/31/2014 11:46 PM, Ben Finney wrote: > > > The ‘max’ function can be told how to determine the ordering of > > items, by specifying a key parameter. The parameter is specified by > > giving a value; that value is a function. > > Hmmm... might just have had a break-thru he

Re: [Tutor] Python functions are first-class citizens

2014-08-01 Thread memilanuk
On 07/31/2014 11:46 PM, Ben Finney wrote: The ‘max’ function can be told how to determine the ordering of items, by specifying a key parameter. The parameter is specified by giving a value; that value is a function. Hmmm... might just have had a break-thru here: so max() iterates thru counts,

Re: [Tutor] Python functions are first-class citizens

2014-07-31 Thread Ben Finney
memilanuk writes: > On 07/31/2014 08:22 PM, Ben Finney wrote: > >> 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

Re: [Tutor] Python functions are first-class citizens

2014-07-31 Thread Alan Gauld
On 01/08/14 06:12, memilanuk wrote: 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. Well done, thats the crux of it.

Re: [Tutor] Python functions are first-class citizens

2014-07-31 Thread memilanuk
On 07/31/2014 08:22 PM, Ben Finney wrote: memilanuk 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;

[Tutor] Python functions are first-class citizens (was: dict.get() vs. dict.get)

2014-07-31 Thread Ben Finney
memilanuk writes: > What is the difference between dict.get() and dict.get The ‘foo()’ syntax calls ‘foo’. ‘dict.get’ is the function (an attribute of the ‘dict’ type), and you can call that function by specifying parameters in parens ‘()’. > counts[words[1]] = counts.get(words[1],