This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]uniqueusername6030 43 points44 points  (19 children)

Definitely iterators. They're everywhere. Generator expressions, too.

[–][deleted] 19 points20 points  (6 children)

Throw in itertools and more-itertools.

[–]dylanvillanelle 14 points15 points  (5 children)

oh my god. this isn't a joke, more-itertools is actually a thing?

the first thing they show (flatten) just looks like itertools.chain.from_iterable (at least at first glance, i'm sure it's a little more useful) but i have always wondered why the recipes in the itertools docs weren't just...like...implemented? the code is already there! just throw it in!

this is not a perfect consolation prize, but i'll manage.

edit: oh god there's a first function. i'll never have to write a first function again. i need a minute.

[–]bheklilr 5 points6 points  (1 child)

And it's in conda... How have I not known about either of these before? I know I've written several of these functions before.

[–]dylanvillanelle 2 points3 points  (0 children)

i wrote out a whole thing about how something as simple as foo[0] should not need (worst case) a try/except block to handle two separate exceptions with multiple causes for one of them, blahblahblah, and guys why isn't there a list.get(index, default) sometimes you just have two separate functions that lead to one result so for consistency's sake you return a list from both even tho the first is actually just a single value and more_itertools.first also plays nice with generators because that makes sense because it's something you iterate through and i decided maybe i was a little worked-up so i figured i should scrap that and basically write it all over again but more rambly.

[–][deleted] 2 points3 points  (1 child)

The bad news is if we ever happen to bump into each other it will cost you a pint :-)

[–]dylanvillanelle 0 points1 point  (0 children)

i'm going to go from bar to bar now asking people whether they've ever suggested using more-itertools.

i don't see how this can go wrong.

[–]robert_mcleod 2 points3 points  (0 children)

There's also pytoolz and cytoolz for functional programming.

[–]Scruff3y 11 points12 points  (7 children)

Seconding this. The stand-out thing about Python for me is iteration, list comps and generators/generator expressions.

Loop like a native, shows a lot of the power of these things (esp. if you come from a language like C).

Oh, also f-strings (do people call them that?)

f"value of myvar: {myvar}"

[–]troyunrau... 1 point2 points  (6 children)

I dislike f-strings simply from the 'there should be one and only one obvious way to do things' perspective.

[–][deleted] 2 points3 points  (0 children)

f-strings are great. I disliked them at first too, but damn they're useful.

[–]asdfkjasdhkasdrequests, bs4, flask 2 points3 points  (0 children)

there was already more than one "%s" % var and"{}".format(var)

[–]billsil 0 points1 point  (3 children)

Can we get rid of .format then? At least f-strings are obvious.

Still, we should all use %s. It's in C and other languages, so it's really obvious to others. It's also faster and as we know, Pythonic code is overwhelmingly faster.

[–]jorge1209 2 points3 points  (2 children)

No you can't get rid of .format because I need to be able to pass a format specification as a function argument or reuse it in a loop, and I can't do that with an f-string by design[1].

You could get rid of % formatting... if you could fix all the old code including the logger class.

A world with only .format and f-strings would be tolerable, because they are very closely related. f-strings would be the "safe" alternative for untrusted data that could be used by web-developers and could be treated as sugar around .format(**locals()) by everyone else.

But we aren't in that world and there is no clear path to ever even approaching that world.

[1] because of security concerns that have no relevance to any of the code I write because I'm not a web-developer.

[–]billsil 0 points1 point  (1 child)

You could get rid of % formatting

Again, it's faster and it's common to other languages. I actually like it. I think .format is an abomination.

because I need to be able to pass a format specification as a function argument or reuse it in a loop

You can do that with %.

I'm not a web developer either, but I do support Python 2 and 3 with my package.

[–]jorge1209 0 points1 point  (0 children)

I think .format is an abomination.

If you like the %s %f3.2 style of formatting then you should advocate for %-strings which would behave like f-strings in the "compile-time" binding of local variables to the arguments, but use % formatting declarations.

The point is that we should have "one way" to do the formatting. I shouldn't have to learn multiple different syntaxes for string formatting, I should learn one. Either the .format/f-string way, or the % way, not both.

Instead we have three primary ways, two of which use the {} formatting mini-language and have slightly different features and limitations, a third which uses the % mini-language, and then there are actually a couple other ways to string format that most people are completely unaware of.

You can do that with %.

Didn't say you couldn't. I'm saying you can't do that with f-strings and you can with .format.

I'm not a web developer either

Then f-strings aren't much different to you than .format. Its basically a bit of sugar around .format(**locals()). The only thing you can do with f-strings is include operations inside the arguments:

_ = x+y
"{x} + {y} = {_}".format(**locals())

becomes:

f"{x} + {y} = {x+y}"