Introducing the "for" loop

Steve D'Aprano steve+python at pearwood.info
Sat Oct 7 23:09:22 EDT 2017


On Sun, 8 Oct 2017 03:58 am, Terry Reedy wrote:


> No built-in function is an instance of FunctionType
>  >>> isinstance(compile, FunctionType)
> False

Ah yes, thanks Terry, I forgot that builtin functions have a distinct type.


[...]
> FunctionType == function defined by def statement or lambda expression.
> These are a subset of functions defined by Python code.

Indeed.


>> It is this last sense (an instance of FunctionType) which people are
>> thinking of when they state that range is not a function.
> 
> Unless one means 'function defined by def or class', excluding all
> functions defined in the interpreter implementation language, which can
> change as modules are recoded one way or the other, and some functions
> defined in Python, FunctionType is too narrow.

Yes, I forgot about the other ways functions can be created, such as partial
objects and builtins and callable instances. (C++ calls them functors.)

However, people often distinguish between functions and methods, since methods
carry their own implicit state ("self") and traditionally functions don't.
But of course the line between them is blurred:

- closures (a kind of function) do carry implicit state;

- static methods in Python (a kind of method) don't carry implicit state.


[...]
> I have the impression that classes being functions is somewhat peculiar
> to Python but I have not been exposed to enough OOP languages to know.

Many languages use function call syntax MyClass(args) (or something quite
close to function call syntax) for instantiating a class. In the sense that a
function is defined by the use of function call syntax, then classes are
functions.

But in the sense that in some languages functions are values but classes are
not (e.g. Java), then classes should be considered distinct from functions.

As always, it depends on what you mean by function.



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




More information about the Python-list mailing list