Live data from Hacker News

Zed finishes Learn Python The Hard Way

sheddingbikes.com

51–60 of 115 posts

Re: Zed finishes Learn Python The Hard Way

#51
post #13

Mongrel 2 and now a book on Python. The man is a machine.

I don't know why he's wasting time teaching python he could make millions (bwahahaha) giving "getting things done" seminars. Of course they would probably consist of him saying "find something that pisses you off.Then fix it" over and over.

I don't know how you got downvoted, but I don't suppose it changes the intent of my reply either way:

That MIGHT just be the secret to Zed's success -- that so much in the world seems to piss him off means that there is so much stuff he feels he needs to fix that he's constantly drowning in the need to fix something.

Some of us are more complacent -- I'm happy to fix the things that irritate me, but my body of work may be less prolific because I'm generally happy with the products that I use.

Re: Zed finishes Learn Python The Hard Way

#52
post #30
post #5

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.

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…

I 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)

Re: Zed finishes Learn Python The Hard Way

#53
post #24

Earlier quoted context omitted.

Asking clear questions will help you learn. You can ask on Stack Overflow (or #python) - make sure to credit the source of your question[0] and at what point you are stuck. Often by trying to explain the precise thing that you don't understand, you will have a moment of zen. [0] People will be a lot more helpful if they know you are trying to learn, rather than posting a "send me teh codes plz" request. Also they wil…

Often by trying to explain the precise thing that you don't understand, you will have a moment of zen. This. I often find that I run into a problem, try to solve it on my own, then bring someone unfamiliar in to get a fresh set of eyes on it. It usually goes like this Me: "Hey, I honestly can't figure out why this is doing this particular thing, can you take a look? ...FUCK! Nevermind, I know exactly whats going on.…

One of my favorite troubleshooting techniques at the office is to have someone SHOW me the problem. Often, in just trying to show me how they're gettingn whatever error they're complaining about, they realize they're doing it wrong in the first place.

Then I get to stamp something 'solved' on my task list, and everybody's happy.

Re: Zed finishes Learn Python The Hard Way

#54
post #39
post #34

Earlier quoted context omitted.

I 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 m…

Don't blame Perl. Blame the programmers and your methodology that allowed them to do that.

You can always delay fixing a social problem by switching technologies.

Re: Zed finishes Learn Python The Hard Way

#55
post #30

Earlier quoted context omitted.

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…

I 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…

Probably 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")]

Re: Zed finishes Learn Python The Hard Way

#56
post #30

Earlier quoted context omitted.

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…

I 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…

[deleted]

Re: Zed finishes Learn Python The Hard Way

#57
post #5

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.

While 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.

There 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.

Re: Zed finishes Learn Python The Hard Way

#59

Earlier quoted context omitted.

I 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…

Probably 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")]

You 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']

Re: Zed finishes Learn Python The Hard Way

#60
post #30

Earlier quoted context omitted.

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…

As 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.

Yes, 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.

Post reply on HN