4 ms·
He 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 __meta
by jerf 16y ago
He 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)
- dchest 16y agoAhem.
- sigzero 16y agoDon't blame Perl. Blame the programmers and your methodology that allowed them to do that.
- chromatic 16y agoYou can always delay fixing a social problem by switching technologies.
- jacobolus 16y agoYou’re implying that language syntax is primarily technical rather than social. I’m not sure I can agree. The reason that Python code uses standard idioms most places you go is that the larger-scale Python community is committed to quite a different set of social norms than the Perl community. Pulling in that language-community momentum can definitely make a difference to the social/technical dynamics inside a smaller group.
- chromatic 16y agoYou’re implying that... ... if you have multiple developers who cannot or will not find some way to write maintainable code, you have a problem independent of language. I've maintained some hideous code written by people who had no business being professional programmers. In every case, adhering to a handful of very simple rules would have improved their code dramatically. Only one case needed a language change, and that one was away from a proprietary 4GL.
- mkramlich 16y agoPerl is a methodology. ;)
- charlief 16y agojerf was being sarcastic and transitioned to his point with "Ahem". Let us all celebrate the ambiguity of text.
- jacobolus 16y agoKeep in mind too that the new class decorators can do a lot of what metaclasses were good for, in a way much easier for the uninitiated to figure out. (Partly because once you understand function decorators class decorators seem obvious and straightforward.) Metaclasses can do some pretty powerful stuff though. One of those features that you might never need, but if you do, you really do.
- yummyfajitas 16y agoAs far as I know, this piece of code (containing a lambda, map and filter) is standard idiomatic python: [ (student.name, 'A') for student in students if student.grade > 90] I'd assert that first class functions are idiomatic python as well and are also critical for python programming. They play an integral part of django, for example.
- jerf 16y agoYes, that's the idiomatic way of doing it, when people complain about lack of map, filter, and lambdas, they're usually missing an opportunity to do it more easily with list comprehensions. Note that doing this with list comprehensions is pretty competitive with Haskell on a keystroke-by-keystroke basis; insisting that your lambdas MUST have the word lambda and the filter MUST have the word filter and that maps MUST have the word map is getting caught up in the surface appearance and missing the deeper flows, which actually, IMHO, isn't a very "functional" attitude to have anyhow. (Functional programming, IMHO, isn't about maps and folds and filters anyhow; what those are are the tools it uses to gain composability. Python uses other tools for that goal; when in Python, you should use those tools. Ultimately, Python is an imperative language and will have all the associated flaws, functional probably does a better job of composability overall. But if you're in Python, you're not going to build a cute monadic combinator library that you can use in Python no matter how hard you try to box it about the ears with map and reduce, so you might as well use what Python actually has, which is classes and generators and list/generator comprehensions and lots of very overloadable syntax and so on and so forth. maps and folds and filters are the surface of functional programming, not the essense.) (And yes, you can build a combinator library, you just can't make it slick.) Reduce is the only one that is not clean in Python ("fold"), and the official word is to use loops. Again, it's usually not that great a loss, but the loss vs Haskell is greatest here. On the other hand, compared against some actually-functional languages you're still not typing much more; Erlang's folds are hardly any better than a Python loop, character-per-character. accum = 0 for i in l: accum += i vs lists:foldl(fun (I, Accum) -> I + Accum end, 0, L) First class-functions are absolutely idiomatic; what's not idiomatic is smashing things together with crazy "and" or "or" chains (or a variety of other hacky approachs) so you can have the magic-goodness-conferring word "lambda" show up in your source, instead of (horrors!) having to give your function a name. Speak the native idioms of the language you are in. A lot of people are missing a lot of the good things about Python because they are too busy complaining that it isn't something that isn't Python. It isn't functional at its core, but it can be a very powerful, concise language, if you work with it instead of fighting it.
- shin_lao 16y agoI thought the book was to be an introduction to programming through Python, that's a good opportunity to show "by the way, you can also see things the functional way...". I'm no Python expert, I couldn't say what's idiomatic or not. That being said, I never had the feeling of struggling with the language when using map and filter, quite the contrary. What's the idiomatic way of doing this? suffix_str_lst = map(lambda x: x + "_suffix", str_lst)
- koenigdavidmj 16y agoProbably to use a list comprehension: suffix_str_lst = [x + "_suffix" for x in str_lst] If you are okay with a generator instead of a list, then you can flip the square brackets to parentheses and then you do not need to hold the whole thing in memory at once. EDIT: You can add a filter in a similar way: suffix_str_list = [x for x in str_list if x.startswith("meep")]
- RyanMcGreal 16y agoYou can also combine maps and filters in a list comprehension: suffix_str_lst = ['{0}_suffix'.format(x) for x in str_list if x[:4] == 'meep']
- zedshaw 16y agoRead that out loud and tell me that doesn't grate on your ears? That's my basic test if code for the book would be understood by the reader. What you have here, this "symbol soup" is what makes programming hard for people in the beginning.
- RyanMcGreal 16y agoI actually find it really elegant. I fell in love with comprehensions as soon as I discovered them. They were the terse, expressive syntax I'd long been searching for.
- angusgr 16y ago