Is Python a functional programming language?

Terry Reedy tjreedy at udel.edu
Sat May 15 18:08:21 EDT 2010


On 5/15/2010 12:42 PM, travis+ml-python at subspacefield.org wrote:

> One very annoying thing in Python is the distinction between
> statements and expressions.

GvR regards it as a feature, claiming that research in the 1980s showed 
that the syntactic heterogeneity aided comprehension. I believe this is 
true for me.

> Ever since learning LISP (well, Scheme) in S&ICP I find myself
> frequently annoyed by this pointless distinction,

You have missed the very important distinction generally reflected in 
the statement/expression divide. Some code is meant to be evaluated 
immediately and some is meant to be quoted for later evaluation. In 
general (yes, there are some exceptions), Python statements are either 
restricted gotos or statements that implicitly quote part of the code in 
the statement. And in general, with a few exceptions, including lambda, 
expressions are completely and immediately evaluated. Changing print and 
exec from statements to functions made Py3 more consistent in this 
respect this distinction.

In List, one either quotes explicitly or must remember that certain 
'functions' are not really functions but are 'special functions' or 
'macros' that implicitly quote the code, just like Python statements do.

Simple statement example:

name = expression

The name is implicitely quoted. Behind the scenes, this is a function 
call. At module level, one can write the call explictly, with explicit 
quotes:

globals().__setitem__('name', expression)

If the statement form annoys you, use the expression equivalent. (Within 
functions, however, you do not have this choice in CPython because, for 
efficiency, function local namespaces are not Python objects and hence 
there is no Python function to directly manipulate them.)

In Lisp, the expression would be something like

(set 'name expression)

Compound statement example:

def f(a, b=3);
	'doc for a'
	<several lines of body code>

The def keyword, default arg expression(s), and doc string are evaluated 
immediately. The function name, parameter names, and body code must be 
quoted somehow. In Python, this is done implicitly as signalled by def 
being a statement keyword rather than a function name. Behind the 
scenes, the def statement is implemented mainly by two function calls: 
compile the (quoted) code and create a function object. I believe that 
one could define functions with explicit calls rather than a statement, 
but it would be much more work and require explicit quotes. Turning a 
class statement into an exec() and type() call is easier, but still 
extra work.

Terry Jan Reedy




More information about the Python-list mailing list