First class lexical closures

J

Jon Harrop

Just debating somewhere else whether or not Python might be considered a
functional programming language. Lua, Ruby and Perl all seem to provide
first class lexical closures.

What is the current state of affairs in Python? Last time I looked they were
just removing (?!) closures...
 
K

Kay Schluehr

Just debating somewhere else whether or not Python might be considered a
functional programming language. Lua, Ruby and Perl all seem to provide
first class lexical closures.

What is the current state of affairs in Python? Last time I looked they were
just removing (?!) closures...

Python has ( ever had ) first class functions. They can be defined
everywhere within each statement and therefore within other function
definitions. They are lexical closures. Pythons support for
*anonymous* closures is somewhat weak. They are not on equal footing
with named functions and they don't play a generative role in the
language ( there is no single piece of language semantics described by
lambda expressions ). There was some willing to remove them in Python
3.0 alltogether. This decision was withdrawn but their power wasn't
extended either.
 
B

Bruno Desthuilliers

Jon Harrop a écrit :
Just debating somewhere else whether or not Python might be considered a
functional programming language. Lua, Ruby and Perl all seem to provide
first class lexical closures.


def makeadder(x):
def add(y):
return x+y
return add


If that's what you mean, then it a common idiom in Python, yes. You may
also want to look for 'function decoators' (some syntactic sugor for
HOFs), and how to define your own callable types (which is just the OO
equivalent to closures).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top