osdir.com


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

Introducing the "for" loop


On Fri, 6 Oct 2017 11:44 pm, ROGER GRAYDON CHRISTMAN wrote:

> Despite the documentation, I would still be tempted to say that range is a
> function.
> 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.

I agree -- range() is a function in the (almost) mathematical sense, something
which takes arguments and returns a value. It's also a type (class), in the
OOP sense:


py> type(range)
<class 'type'>


The term "function" is ambiguous but normally clear from context. Often, the
differences make no difference, but when they are important, we can discuss
them:

- range is a callable (a callable object);

- it is also a type/class, and calling it returns an instance;

- it looks like, and behaves like, a function;

- and is, conceptually, a function;

- but it is *not* an instance of FunctionType:


py> from types import FunctionType
py> def function():
...     pass
...
py> isinstance(function, FunctionType)
True
py> isinstance(range, FunctionType)
False


It is this last sense (an instance of FunctionType) which people are thinking
of when they state that range is not a function.



-- 
Steve
?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure
enough, things got worse.