Isn’t that already happening? I’ve seen countless suggestions getting rejected for such reasons.
Python has no such philosophy. Many people believe that it does, because of their interpretation of the line in the “Zen of Python”, but the Python core developers’ interpretation seems quite different. As expressed in PEP 584:
There is no [Only One Way] koan
There is no [more than one way to do it] prohibition
If you’re looking for a language that comes closer to your desired philosophy of “one way of doing things”, perhaps consider Go?
(my emphasis).
As a puzzle, expect to find golfing, obfuscations, and scarcely used /often misunderstood areas of Python..
@bluelobe, were there some instructions provided along with the code that you could share with us? We would like to know the details of the challenge that was being posed.
@bluelobe Would be good to share a link to the tweet.
x[::-1] and reversed(x) are similar, but they have some important differences. The first form preserves the type of the sequence and is thus convenient if you want, for example, reverse a string or a tuple:
>>> 'hello'[::-1]
'olleh'
>>> (1, 2, 3)[::-1]
(3, 2, 1)
If you use reversed(), you get a reversed object that you then need to convert yourself:
>>> reversed('hello')
<reversed object at 0x7f0d88933af0>
>>> ''.join(reversed('hello'))
'olleh'
>>> tuple(reversed((1, 2, 3)))
(3, 2, 1)
A benefit of reversed() is that it just iterates items from the end of sequence and doesn’t need to recreate the whole sequence in memory. This doesn’t matter with the above examples, but with large lists and other sequences the difference can be significant over using the slice notation.
In other words, there are use cases where x[::-1] works better and use cases where reversed(x) works better. In my opinion Python would be a poorer language if it only had one of them. I’m not sure is it necessary for reversed to be a built-in, but if it needed to be imported, many users would probably use the slice notation even in places where reversed would be better.
Not true already for lists. You get a list_reverseiterator object instead. But yes, an iterator in either case, and that has different features/advantages. Besides memory usage, it can also be a lot faster (for example to find the last index of a list element) and can be used by multiple consumers for shared iteration (like zip(it, it) to get pairs, or more elaborate cases that use the same iterator in several places, like those where the itertools docs call iter(...)).
True, but if you have other indexings all over your code like L[::-2], the most obvious way might be [::-1] over reversed, for the sake of unfoolish consistency. Note that the zen of python does not say “there MUST be…”.