osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Introducing the "for" loop


On 10/6/2017 8:44 AM, ROGER GRAYDON CHRISTMAN wrote:

> Despite the documentation, I would still be tempted to say that range is a
> function.

It is, *according* to the documentation.  Built-in classes are included 
in Library Reference, Ch. 2, Built-in Functions.  Changing that to 
"Built-in Functions and Classes" has been proposed (perhaps by me) and 
rejected.

> Taking duck-typing to the meta-level, every time I use range, I use its name
> followed
> by a pair of parentheses enclosing one to three parameters, and I get back an
> immutable sequence object.   It sure looks like a function to me.

In the standard mathematical sense it is, more so than instances of 
classes <function> or <built-in function), such as print, that operate 
mainly by side effects.

> I would similarly say that map is a function, or an iterator generator
> I write myself with yield statements is a function,

Functions with 'yield' are 'generator functions'

> both of which also return sequences.

Iterators are mutable representatives of sequences, but cannot be indexed.

> It is not clear to me what the difference really is between my conception
> and the official definition -- is it a question about whether it returns a
> first-class object?

No.  All python callables (functions in the broad sense) return objects.

> Or more simply, what is the clear and obvious distinction that I can give to my
> non-scientific laypeople about why range isn't a function, if it looks like one?

It is a function, in the usual sense not specific to Python, that is 
specifically a Python class.  It is not just a function.  It is a 
function that returns an instance of itself when called.  This is the 
distinguishing feature of classes as functions (callables).

-- 
Terry Jan Reedy