[Tutor] Python functions are first-class citizens

Alan Gauld alan.gauld at btinternet.com
Fri Aug 1 23:26:22 CEST 2014


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 iterator.
The docs say iterator not collection.
The max function works with generators which are not collections.

Here is an example:

 >>> def mycount(first,limit):
...    n=first
...    while n < limit:
...       yield n
...       n += 1
...
 >>> for x in mycount(1,5):
...    print x
...
1
2
3
4
 >>> max(mycount(2,5))
4
 >>>

You don't need a collection to call max()

> though; I would prefer instead that it says “collection”, because
> whether the collection is iterable shouldn't matter.

But the docs say it does matter. The specification of max() is what the 
docs say, and they say it should be an iterable. I don't know what 
happens if you pass a non-iterable collection but the docs do say it 
should be iterable and assuming that it might work with a non-iterable 
is the mistake...


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list