Is Python a functional programming language?

A

Aahz

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

One extremely valuable thing in Python is the distinction between
statements and expressions.

In fact, given the semantic similarity between Python and Lisp, I would
argue that Python having the distinction is partly responsible for its
popularity.
 
T

Terry Reedy

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
 
P

Paul Rubin

To be fair, it appears that Python's whitespace-sensitive syntax sort
of precludes the "make a complex function on one line" that is typical
of languages which don't have statement/expression distinctions, but
I'm not convinced it couldn't be solved, perhaps by allowing anonymous
functions to span multiple lines, just like named functions.

Haskell has whitespace-based syntax like Python (you can alternatively
use curly braces). Their term for it is "layout". You can
alternatively use curly braces and semi-colons. I'd have to say that
Haskell's indentation rules are a bit harder to understand than Python's
at first.
 
T

Teemu Likonen

PS: Why do people call LISP object-oriented? Are they smoking crack?
No classes, no methods, no member variables... WTF?

Maybe because Common Lisp has a strong support for object-oriented
programming.


Peter Seibel: Practical Common Lisp

Generic functions and methods (chapter 16)
http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html

Classes (chapter 17)
http://www.gigamonkeys.com/book/object-reorientation-classes.html

Paul Graham: On Lisp

Object-Oriented Lisp (chapter 25)
http://www.bookshelf.jp/texi/onlisp/onlisp_26.html#SEC156

Wikipedia: Common Lisp Object System

http://en.wikipedia.org/wiki/Common_Lisp_Object_System
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top