---
created_at: '2017-01-09T01:13:48.000Z'
title: Origins of Python's “Functional” Features (2009)
url: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
author: tosh
points: 127
story_text: 
comment_text: 
num_comments: 34
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1483924428
_tags:
- story
- author_tosh
- story_13353266
objectID: '13353266'

---
[Source](http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html "Permalink to The History of Python: Origins of Python's "Functional" Features")

# The History of Python: Origins of Python's "Functional" Features

[skip to main ][1] | [skip to sidebar][2]

#  [ The History of Python ][3]

A series of articles on the history of the Python programming language and its community.

## Tuesday, April 21, 2009

###  Origins of Python's "Functional" Features 

I have never considered Python to be heavily influenced by functional languages, no matter what people say or think. I was much more familiar with imperative languages such as C and Algol 68 and although I had made functions first-class objects, I didn't view Python as a functional programming language. However, earlier on, it was clear that users wanted to do much more with lists and functions.  
  
A common operation on lists was that of mapping a function to each of the elements of a list and creating a new list. For example:  

> 
>     def square(x):
>         return x*x
>     
>     vals = [1, 2, 3, 4]
>     newvals = []
>     for v in vals:
>         newvals.append(square(v))

  
In functional languages such as Lisp and Scheme, operations such as this were provided as built-in functions of the language. Thus, early users familiar with such languages found themselves implementing comparable functionality in Python. For example:  

> 
>     def map(f, s):
>         result = []
>         for x in s:
>                 result.append(f(x))
>         return result
>     
>     def square(x):
>         return x*x
>     
>     vals = [1, 2, 3, 4]
>     newvals = map(square,vals)

  
A subtle aspect of the above code is that many people didn't like the fact that you to define the operation that you were applying to the list elements as a completely separate function. Languages such as Lisp allowed functions to simply be defined "on-the-fly" when making the map function call. For example, in Scheme you can create anonymous functions and perform mapping operations in a single expression using lambda, like this:  

> 
>     (map (lambda (x) (* x x)) '(1 2 3 4))  

  
Although Python made functions first-class objects, it didn't have any similar mechanism for creating anonymous functions.  
  
In late 1993, users had been throwing around various ideas for creating anonymous functions as well as various list manipulation functions such as map(), filter(), and reduce(). For example, Mark Lutz (author of "Programming Python") posted some code for a function that created functions using exec:  

> 
>     def genfunc(args, expr):
>         exec('def f(' + args + '): return ' + expr)
>         return eval('f')
>     
>     # Sample usage
>     vals = [1, 2, 3, 4]
>     newvals = map(genfunc('x', 'x*x'), vals)

  
Tim Peters then followed up with a solution that simplified the syntax somewhat, allowing users to type the following:  

> 
>     vals = [1, 2, 3, 4]
>     newvals = map(func('x: x*x'), vals)

  
It was clear that there was a demand for such functionality. However, at the same time, it seemed pretty "hacky" to be specifying anonymous functions as code strings that you had to manually process through exec. Thus, in January, 1994, the map(), filter(), and reduce() functions were added to the standard library. In addition, the lambda operator was introduced for creating anonymous functions (as expressions) in a more straightforward syntax. For example:  

> 
>     vals = [1, 2, 3, 4]
>     newvals = map(lambda x:x*x, vals)

  
These additions represented a significant, early chunk of contributed code. Unfortunately I don't recall the author, and the SVN logs don't record this. If it's yours, leave a comment! **UPDATE: **As is clear from Misc/HISTORY in the repo these were contributed by Amrit Prem, a prolific early contributor.  
  
I was never all that happy with the use of the "lambda" terminology, but for lack of a better and obvious alternative, it was adopted for Python. After all, it was the choice of the now anonymous contributor, and at the time big changes required much less discussion than nowadays, [for better][4] and [for worse][5].  
  
Lambda was really only intended to be a syntactic tool for defining anonymous functions. However, the choice of terminology had many unintended consequences. For instance, users familiar with functional languages expected the semantics of lambda to match that of other languages. As a result, they found Python’s implementation to be sorely lacking in advanced features. For example, a subtle problem with lambda is that the expression supplied couldn't refer to variables in the surrounding scope. For example, if you had this code, the map() function would break because the lambda function would run with an undefined reference to the variable 'a'.  

> 
>     def spam(s):
>         a = 4
>         r = map(lambda x: a*x, s)

  
There were workarounds to this problem, but they counter-intuitively involved setting default arguments and passing hidden arguments into the lambda expression. For example:  

> 
>     def spam(s):
>         a = 4
>         r = map(lambda x, a=a: a*x, s)

  
The "correct" solution to this problem was for inner functions to implicitly carry references to all of the local variables in the surrounding scope that are referenced by the function. This is known as a "closure" and is an essential aspect of functional languages. However, this capability was not introduced in Python until the release of version 2.2 (though it could be imported "from the future" in Python 2.1).  
  
Curiously, the map, filter, and reduce functions that originally motivated the introduction of lambda and other functional features have to a large extent been superseded by list comprehensions and generator expressions. In fact, the reduce function was removed from list of builtin functions in Python 3.0. (However, it's not necessary to send in complaints about the removal of lambda, map or filter: they are staying. :-)  
  
It is also worth nothing that even though I didn't envision Python as a functional language, the introduction of closures has been useful in the development of many other advanced programming features. For example, certain aspects of new-style classes, decorators, and other modern features rely upon this capability.  
  
Lastly, even though a number of functional programming features have been introduced over the years, Python still lacks certain features found in “real” functional programming languages. For instance, Python does not perform certain kinds of optimizations (e.g., tail recursion). In general, because Python's extremely dynamic nature, it is impossible to do the kind of compile-time optimization known from functional languages like Haskell or ML. And [that's fine][6]. 

Posted by  [ Guido van Rossum ][7] at  [11:05 AM][8] [ ![][9] ][10]

#### 21 comments:

1. ![][11]

[Ceyhun Alyeşil][12][April 21, 2009 at 11:44 AM][13]

back to python now. miss its functions :D

[Reply][14][Delete][15]

2. ![][16]

[D'gou][17][April 21, 2009 at 11:55 AM][18]

Really, Python is so dynamic that it can't tell when 'return' is returning a function call and thus can't do tail-call optimization?  
  
Or is it unneeded/undesirable for some particular philosophical reason? (I am not familiar with all the stackless vs. core Python issues, so maybe that could make a future post, how the philosophies involved are incompatible)...  
  
Thank you for writing all of this down, it is a lot more important than it might seem at the time...  
  
-Doug

[Reply][14][Delete][19]

3. ![][20]

[wesley chun][21][April 21, 2009 at 11:56 AM][22]

it's a shame that blogspot (at the time of this writing) does not preserve the whitespace indentation that's necessary for Python code. :P

[Reply][14][Delete][23]

4. ![][24]

[Guido van Rossum][25][April 21, 2009 at 12:07 PM][26]

Argh. Fixed the whitespace.  
  
Regarding tail optimization: that's a long topic, maybe I'll get to it in a future post on my other blog.

[Reply][14][Delete][27]

5. ![][28]

[Ivo][29][April 21, 2009 at 12:11 PM][30]

You could fix the whitespaces in CSS:  
  
blockquote {  
white-space: pre;  
}

[Reply][14][Delete][31]

6. ![][32]

[juhan][33][April 21, 2009 at 12:11 PM][34]

but? missing indentation in examples?

[Reply][14][Delete][35]

7. ![][28]

[Alex][36][April 21, 2009 at 12:16 PM][37]

You think Python could detect tail recusion, until you find out someone monkey patched your function into something else in a different thread my eval'ing an opaque string.

[Reply][14][Delete][38]

8. ![][28]

[Fred Blasdel][39][April 21, 2009 at 4:20 PM][40]

I hate that Python doesn't have Tail-Call Optimization, though it does have the best stack traces of any language I've seen, which mostly makes up for it.  
  
Not being able to cleverly recurse isn't so bad when you never have to deal with anonymous "you tried to get the head of an empty list" exceptions!

[Reply][14][Delete][41]

9. ![][28]

[nathany][42][April 21, 2009 at 8:05 PM][43]

Lua manages to have Tail-Call Optimization in a dynamic language, though I honestly don't know how much I'd use it.

[Reply][14][Delete][44]

10. ![][16]

[fiber-space][45][April 22, 2009 at 1:26 AM][46]

Seems like this is the TCO week for Python. Two blog posts that discussed this topic were published independently about the same time on Monday. This is now the third one.  
  
http://www.teamrubber.com/blog/python-tail-optimisation-using-byteplay/  
http://fiber-space.de/wordpress/?p=349

[Reply][14][Delete][47]

11. ![][16]

[regionmap][48][April 22, 2009 at 3:40 AM][49]

you article is very interesting for me

[Reply][14][Delete][50]

12. ![][51]

[Robert Virding][52][April 22, 2009 at 5:17 AM][53]

I don't see why Python's dynamic nature would make it impossible to do TCO, after all Erlang can do it and it is just as dynamic.

[Reply][14][Delete][54]

13. ![][16]

[D'gou][17][April 22, 2009 at 6:52 AM][55]

@Alex could you be a bit more specific. Evaling a 'def ...' or a 'module.thing = lambda ...' won't change a function definition, it will provide a new function definition that might be bound to the same name. I don't know enough Python internals to know how much the actual byte code of a function can be changed (I had been assuming those are immutable, same as string contents).

[Reply][14][Delete][56]

14. ![][24]

[Guido van Rossum][25][April 22, 2009 at 1:57 PM][57]

New blog post in response to the tail recursion discussion: http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html

[Reply][14][Delete][58]

15. ![][28]

[johnnyg][59][April 26, 2009 at 2:53 AM][60]

How appropriate that the anonymous function patch is itself anonymous.

[Reply][14][Delete][61]

16. ![][16]

[reinierpost][62][May 19, 2009 at 2:13 AM][63]

Perhaps it's worth pointing out that Algol 68 has anonymous functions (but no closures).

[Reply][14][Delete][64]

17. ![][24]

[Guido van Rossum][25][May 19, 2009 at 6:56 AM][65]

@reinierpost: really? No closures in Algol 68? What's your definition of closures? I'm pretty sure functions can reference variables in outer scopes, and functon objects can be passed around. What's missing for "closures"?

[Reply][14][Delete][66]

18. ![][28]

[NevilleDNZ][67][May 24, 2009 at 5:03 AM][68]

re: Closures - Compare Python to Algol 68...  
# Python #  
from math import *  
root = lambda x: lambda y: exp(log(y)/x);  
square_root = root(2); cube_root = root(3);  
print square_root(64), cube_root(64)  
# Algol 68 #  
MODE F = PROC(REAL)REAL;  
PROC root = (REAL x)F: (REAL y)REAL: exp(ln(y)/x);  
F square root = root(2), cube root = root(3);  
print((square root(64), cube root(64), new line))  
Output: 8.0 4.0  
  
The key point about scoping is that with Algol 68 each argument "x" exists only on the LOC stack. Hence when "root" returns "x" becomes out of scope (off the stack). In python each "x" is in HEAP, then "follows" square_root and cube_root until these functions are no longer referenced.  
  
Quick fix for Algol 68 could be to put x on the HEAP eg.  
PROC root = (HEAP REAL x)F: (REAL y)REAL: exp(ln(y)/x);  
And let the garbage collector sort out x's closure.  
  
Minor other differences: Algol 68 is "IMPLICIT NONE", (useful for picking up bugs prior to runtime).

[Reply][14][Delete][69]

19. ![][24]

[Guido van Rossum][25][May 24, 2009 at 11:55 AM][70]

Neville, this probably doesn't belong in this comment thread any more, but I'm not sure I understand. Are you saying that the Algol68 example doesn't work? Can you mail guido@python.org so we can sort this out a bit more efficiently than in a comment thread? (I don't have an a68 compiler handy to experiment, sorry. :-)

[Reply][14][Delete][71]

20. ![][16]

[reinierpost][62][July 6, 2009 at 8:52 AM][72]

Guido: one thing that blew up for me was returning functions. This is confirmed at   
  
http://enthusiasm.cozy.org/archives/2005/04/original-closures

[Reply][14][Delete][73]

21. ![][28]

[Unknown][74][December 1, 2013 at 8:27 PM][75]

Hi, Guido :-) Misc/HISTORY records that lambda et alia were introduced in Python release 1.0.0 (26 January 1994), and that the code was contributed my Amrit Prem.

[Reply][14][Delete][76]

[Add comment][14]

[Load more...][14]

Note: Only a member of this blog may post a comment.

[Newer Post][77] [Older Post][78] [Home][3]

Subscribe to: [Post Comments (Atom)][79]

## Followers

[ ![][80] ][81]

## Blog Archive

* [ ►  ][82] [ 2013 ][83] (4)
    * [ ►  ][82] [ November ][84] (2)
    * [ ►  ][82] [ October ][85] (2)
* [ ►  ][82] [ 2011 ][86] (1)
    * [ ►  ][82] [ July ][87] (1)
* [ ►  ][82] [ 2010 ][88] (7)
    * [ ►  ][82] [ August ][89] (1)
    * [ ►  ][82] [ June ][90] (6)
* [ ▼  ][82] [ 2009 ][91] (19)
    * [ ▼  ][82] [ April ][92] (4)
        * [Metaclasses and Extension Classes (a.k.a. “The Kil...][93]
        * [New! Now in Japanese!][94]
        * [And the Snake Attacks][95]
        * [Origins of Python's "Functional" Features][96]
    * [ ►  ][82] [ March ][97] (5)
    * [ ►  ][82] [ February ][98] (4)
    * [ ►  ][82] [ January ][99] (6)

[ ![][80] ][100]

## About Me

![My photo][101]

[ Guido van Rossum ][25]
: Python's BDFL

[View my complete profile][25]

[ ![][80] ][102]

 

[1]: http://python-history.blogspot.com#main
[2]: http://python-history.blogspot.com#sidebar
[3]: http://python-history.blogspot.com/
[4]: http://www.python.org/dev/peps/pep-0001/
[5]: http://yellow.bikeshed.com/
[6]: http://hugunin.net/story_of_jython.html
[7]: https://www.blogger.com/profile/12821714508588242516 "author profile"
[8]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html "permanent link"
[9]: https://resources.blogblog.com/img/icon18_edit_allbkg.gif
[10]: https://www.blogger.com/post-edit.g?blogID=8699431508730375743&postID=5909934651334026902&from=pencil "Edit Post"
[11]: http://3.bp.blogspot.com/_jV_yENp-_Zk/SXpj-34LZdI/AAAAAAAAALs/Cpsf925ZgGo/S45-s35/benrh7.jpg
[12]: https://www.blogger.com/profile/12056513458669321101
[13]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240339440000#c7901786831401354811
[14]: javascript:%3B
[15]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=7901786831401354811
[16]: http://lh3.googleusercontent.com/Rw_wdkZHfDXh8tDEBFF1Q3aSS_nDE-gMVWNfOHb7a2nIp2XuWAmJyGISHv9akZE%3Ds35
[17]: http://dgou.myopenid.com/
[18]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240340100000#c8398278051084156114
[19]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=8398278051084156114
[20]: http://2.bp.blogspot.com/_qe8XI1VUs6Q/SXqckYOUpCI/AAAAAAAAABE/Fgz1-8Rk8Ck/S45-s35/coolme-thb.jpg
[21]: https://www.blogger.com/profile/08896306361304265422
[22]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240340160000#c5418398309299673375
[23]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=5418398309299673375
[24]: http://2.bp.blogspot.com/_FG9t5W1SJ14/SZdIM5ZxpgI/AAAAAAAAD5Q/49Kj8K7N2Og/S45-s35/IMG_2192.jpg
[25]: https://www.blogger.com/profile/12821714508588242516
[26]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240340820000#c8884103572551745065
[27]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=8884103572551745065
[28]: http://lh3.googleusercontent.com/zFdxGE77vvD2w5xHy6jkVuElKv-U9_9qLkRYK8OnbDeJPtjSZ82UPq5w6hJ-SA%3Ds35
[29]: https://www.blogger.com/profile/12732454351811400017
[30]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240341060000#c8929783183558852154
[31]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=8929783183558852154
[32]: http://3.bp.blogspot.com/_JB5MwWVLeD4/SczV0owinBI/AAAAAAAAAAs/jyjagzRlwt8/S45-s35/jldp.jpg
[33]: https://www.blogger.com/profile/07466683261343797955
[34]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240341060001#c4777482829236795300
[35]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=4777482829236795300
[36]: https://www.blogger.com/profile/14054821112394577330
[37]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240341360000#c9162038581005456447
[38]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=9162038581005456447
[39]: https://www.blogger.com/profile/08057528812732998703
[40]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240356000000#c2052755451158154960
[41]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=2052755451158154960
[42]: https://www.blogger.com/profile/17602902852018295760
[43]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240369500000#c2270019218628260582
[44]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=2270019218628260582
[45]: http://fiber-space.myopenid.com/
[46]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240388760000#c4829481812387355188
[47]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=4829481812387355188
[48]: http://regionmap.blogspot.com/
[49]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240396800000#c591910858886735075
[50]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=591910858886735075
[51]: http://4.bp.blogspot.com/_ikZaw5hU8VI/SaI9JGgTiOI/AAAAAAAAAAk/V7BfSd8epLY/S45-s35/Robert%2Bi%2Bbadet.jpg
[52]: https://www.blogger.com/profile/16187415127098159771
[53]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240402620000#c4364360192602680547
[54]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=4364360192602680547
[55]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240408320000#c5291932089952782640
[56]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=5291932089952782640
[57]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240433820000#c3617430713054451807
[58]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=3617430713054451807
[59]: https://www.blogger.com/profile/13739788199470046447
[60]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1240739580000#c8993673962342443643
[61]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=8993673962342443643
[62]: http://reinierpost.myopenid.com/
[63]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1242724380000#c9060025465132625137
[64]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=9060025465132625137
[65]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1242741360000#c1250737883053776454
[66]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=1250737883053776454
[67]: https://www.blogger.com/profile/11067569438021395601
[68]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1243166621952#c702829329923892023
[69]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=702829329923892023
[70]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1243191318922#c5273711396554100653
[71]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=5273711396554100653
[72]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1246895572078#c5998315990217476906
[73]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=5998315990217476906
[74]: https://www.blogger.com/profile/15023674227978538137
[75]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?showComment=1385958424763#c3846315993978012555
[76]: https://www.blogger.com/delete-comment.g?blogID=8699431508730375743&postID=3846315993978012555
[77]: http://python-history.blogspot.com/2009/04/and-snake-attacks.html "Newer Post"
[78]: http://python-history.blogspot.com/2009/03/great-or-grand-renaming.html "Older Post"
[79]: http://python-history.blogspot.com/feeds/5909934651334026902/comments/default
[80]: https://resources.blogblog.com/img/icon18_wrench_allbkg.png
[81]: //www.blogger.com/rearrange?blogID=8699431508730375743&widgetType=Followers&widgetId=Followers2&action=editWidget&sectionId=sidebar "Edit"
[82]: javascript:void(0)
[83]: http://python-history.blogspot.com/2013/
[84]: http://python-history.blogspot.com/2013/11/
[85]: http://python-history.blogspot.com/2013/10/
[86]: http://python-history.blogspot.com/2011/
[87]: http://python-history.blogspot.com/2011/07/
[88]: http://python-history.blogspot.com/2010/
[89]: http://python-history.blogspot.com/2010/08/
[90]: http://python-history.blogspot.com/2010/06/
[91]: http://python-history.blogspot.com/2009/
[92]: http://python-history.blogspot.com/2009/04/
[93]: http://python-history.blogspot.com/2009/04/metaclasses-and-extension-classes-aka.html
[94]: http://python-history.blogspot.com/2009/04/new-now-in-japanese.html
[95]: http://python-history.blogspot.com/2009/04/and-snake-attacks.html
[96]: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
[97]: http://python-history.blogspot.com/2009/03/
[98]: http://python-history.blogspot.com/2009/02/
[99]: http://python-history.blogspot.com/2009/01/
[100]: //www.blogger.com/rearrange?blogID=8699431508730375743&widgetType=BlogArchive&widgetId=BlogArchive2&action=editWidget&sectionId=sidebar "Edit"
[101]: http://2.bp.blogspot.com/_FG9t5W1SJ14/SO0aRdEpTAI/AAAAAAAACvw/sQy2btDo2DI/S220-s80/IMG_2192.jpg
[102]: //www.blogger.com/rearrange?blogID=8699431508730375743&widgetType=Profile&widgetId=Profile2&action=editWidget&sectionId=sidebar "Edit"

  [*[11:05 AM]: 2009-04-21T11:05:00-07:00

