Live data from Hacker News

A Python Guide for the Ages

gto76.github.io

1–10 of 59 posts

Re: A Python Guide for the Ages

#3
There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

Re: A Python Guide for the Ages

#4

There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

I actually saw someone use that for-else pattern in advent of code earlier this month. I believe the consensus was that it was definitely the perfect choice for the given use case, but neither he nor I would ever dare use it in production because nobody knows about it and it's super unintuitive.

I think the idea is cool (albeit rarely useful), but a different word than "else" for the same functionality would go a long way.

Re: A Python Guide for the Ages

#5
Previous substantial discussion: https://news.ycombinator.com/item?id=19075325 (Feb 4, 2019 | 81 comments)

Other cheatsheets (excluding learnxinyminutes already mentioned in another comment)

* Python Crash Course: https://ehmatthes.github.io/pcc_2e/cheat_sheets/cheat_sheets...

* Scientific Python: https://ipgp.github.io/scientific_python_cheat_sheet/

Re: A Python Guide for the Ages

#7

There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

I've used both, for with else more often. It avoided some booleans and if's that would have been much less clear/easy to get wrong.

I'd like a different name for for "for's" else.

I've been using method/function redefinition in place of conditionals related to initialization.

Re: A Python Guide for the Ages

#8
post #7

There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

I've used both, for with else more often. It avoided some booleans and if's that would have been much less clear/easy to get wrong. I'd like a different name for for "for's" else. I've been using method/function redefinition in place of conditionals related to initialization.

It’s consistent with try…else and if…else so I actually think it’s ok.

Re: A Python Guide for the Ages

#9
post #7

Earlier quoted context omitted.

I've used both, for with else more often. It avoided some booleans and if's that would have been much less clear/easy to get wrong. I'd like a different name for for "for's" else. I've been using method/function redefinition in place of conditionals related to initialization.

It’s consistent with try…else and if…else so I actually think it’s ok.

"else" usually means "instead of" or "otherwise" in those patterns. If the original case doesn't run, then the else case runs instead.

In a for loop, the else clause only runs if the loop successfully completes (isn't broken), so the else in for-else means the total opposite of what it means in every other pattern.

I think it would make a lot more sense if it were replaced with "done" or "upon" or something else that communicates how it works.

Re: A Python Guide for the Ages

#10

There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

I prefer using the else clause to maintaining an "is_found" flag. Although this is lesser known syntax so I probably wouldn't use it when multiple developers are involved.
Post reply on HN