7 ms·
Unless I'm mistaken there's no chapter about functional programming in Python (lambda functions, filter(), map() and reduce()). I really think it's acceptable
by shin_lao 16y ago
Unless I'm mistaken there's no chapter about functional programming in Python (lambda functions, filter(), map() and reduce()).
I really think it's acceptable to introduce these function to newbies, I'll go as far as saying it's actually a good thing.
I like the chapter on automated testing.
- charlief 16y agoWhile I do agree those principles are useful, I think introducing those topics would diverge the focus of the book. I think the goal was of introductory nature not comprehensiveness.
- danieldk 16y agoThere are some great beginner's books out there that barely cover functional constructs (e.g. PragProg's 'Learn to Program'). But I have always felt that such an omission is really a bad mistake. map/fold/filter introduce two very important ideas: - Higher-order functions - Many tasks are just list transformations What makes things more painful is that Java is so omnipresent in CS education. Since Java only contains such concepts implicitly (and require a lot of verbosity), I haven't encountered many (if any) Java books that go into functional concepts. It's like learning UNIX without covering pipes. Sure, you can work with UNIX, inadvertently emulate it using redirections from and to files. But it will never be as effective.
- zedshaw 16y ago"Higher-order functions, ... list transformations" you just said four words that are an entire book on their own.
- danieldk 16y agoSure, that's why Graham Hutton can do it in one chapter. Most books discuss lists and functions anyway, and from there is not such a big step to give an introduction to lambdas/closures, maps, folds, and filters. It's not as if we are discussing monads (which are also easy to understand, given a good teacher).
- zedshaw 16y agoDoubt it. He may have used a chapter of text to explain something, but who he explained it to probably could understand it in a chapter of text.
- steveklabnik 16y agoThe Python community has frowned on functional programming, reduce isn't in basic Python anymore, and they almost took out lambda: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 http://www.artima.com/weblogs/viewpost.jsp?thread=98196
- RyanMcGreal 16y agoStrange that they took out reduce() but not map() and filter(), which you can easily replicate with list comprehensions.
- steveklabnik 16y agoYeah. The argument is based on reduce not being clear and easy to understand, which is frustrating to those of us who actually find map/reduce/filter to be more clear, not less.
- ubernostrum 16y agoreduce isn't in basic Python anymore It's in a module in the standard library, a module which contains a bunch of tools for working with higher-order functions. Or, in other words, if you've got a Python 3.x installed, you've got a reduce function available. The only difference is that it's no longer in the default global namespace (map and filter still are, of course). This matches up pretty cleanly with my actual experience of writing Python; I've written a hell of a lot of Python code these past 6-7 years and so far as I can recall I've never used reduce. (and if you want to get picky about this, keep in mind how much of Haskell requires import statements to get at -- moving less-commonly-used stuff into standard modules is not a bad thing at all)
- tdavis 16y agoDitto for generators. In the process you could introduce pipelining and thus give a window into the Unix way which is probably pretty relevant for a newcomer. Or I've just been around too long to know what's relevant for a newcomer.
- jokull 16y agoYeah generators are awesome. +1 for a chapter on them with small sprinkles of functional programming.
- jerf 16y agoHe didn't cover defining first-class functions that I could see in a quick check, didn't cover generators, and I think most disappointingly, didn't cover __metaclass__. That's some critical stuff for Python programming right there, everybody needs __metaclass__. Ahem. You have to pick what to cover. For a beginning Python book, I'm underwhelmed by the need to drag an entirely new and foreign(-to-Python) paradigm into the beginning Python. I say this as someone who does a lot of functional programming, someone who is using a lot of lambda, filter, map, and reduce is someone who is stubbornly refusing to program with the native idioms, which are named functions (defined inline is fine), list comprehension or loop, list comprehension or loop, and loop respectively. It is not that enormous a gain, and will lose from Python fighting you all the way. (Plus you'll probably miss some opportunities to use generators, overload the special methods, and the other things you can do with native idioms, if you're trying to program Haskell in Python.)
- sharms 16y agoI have programmed literally hundreds of thousands of lines of python without using classes or __metaclass__, as a counter example. When dealing in industry, we can't always hire the best talent, so there is no need to make things overly complex. Simple solutions work best for a lot of scenarios, and I haven't been limited by it yet. Not to bash Perl, but the whole reason we write in Python is because there were too many people on our team writing in Perl using too many obscure constructs that took too long to review / revise (especially years later when team members changed and code was forgotten in parts)
- araneae 16y agoMeh. I just sent this to a friend of mine that knows nothing about coding, but is taking a class (biology) that uses python. She really has no need of functional programming, she just needs to know how to survive her problem sets. I think that's the audience.
- zedshaw 16y agoExactly. I taught programming, not dogma. Frankly, if I can teach two paradigms, and one is easy to explain while the other is hard, well I'm going to teach the easy one to explain. Functional programming is just hard to explain to people who know next to nothing. Especially in a language like Python where the functional concepts are kind of half-assed.
- zedshaw 16y agoYou are not mistaken, there is no chapter on functional programming. Why? Because "The Little Schemer" is a way better book for teaching better functional programming than I could possibly do in a Python book. Python's functional concepts are just not really that great, so I couldn't explain them very well.
- illumen 16y agoIf you're not teaching pythons functional concepts, then you're not teaching python. However I think you have accidentally... consider this short example which shows off first class functions, map, reduce, no side effects, and filtering. [(lambda a,b,p:(a+p,b+p))(x,y,2) for x in (1,4) for y in (10,15,22) if x*y > 25] Python does functional programming without people even knowing that it is functional programming :)