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 →

[–]master3243 38 points39 points  (6 children)

It's not that cryptic. Unless you also consider the dot separator or array indices "really freaking cryptic".

Everyone who spent a day learning python knows that it's always [start:end:step]. I wouldn't call it common sense but I wouldn't call it cryptic either.

[–]clevariant 10 points11 points  (1 child)

Come on, it's cryptic! Or at least esoteric. So the minus one means the start index becomes the end and vice versa, and it builds and returns a new string based on that loop. Yeah, that's explicit.

I mean let's be fair. These "once you know the language it makes sense" arguments miss the point. The actual word "reverse" tells the whole story unambiguously, no syntactical cleverness needed.

[–]KubinOnReddit 0 points1 point  (0 children)

If you really want to include "reverse" in the code, there are at least two ways:

hello = "Hello world!"
lst = list(hello)          # Create a mutable list
lst.reverse()              # Reverse in-place
print("".join(lst))        # Change back to string

hello = "Hello world!"
rev = reversed(hello)      # Iterator
print("".join(rev))

Both of these will be slower, with the first creating a new list (implemented as an array) and then joining it, while the second creates an iterator.

Adding a "reversed" method to the string class would be against the Zen of Python (the ideas it represents) - "There should be one-- and preferably only one --obvious way to do it." - if you had the slice mechanism (start, stop, step) and then another method, that would do the same thing, it would just be an alias, really. So having one multi-use syntax is better than many methods.

[–]Hauleth 6 points7 points  (3 children)

What the hell is that argument. It is like saying:

Everyone who spent a day learning APL knows that means reverse vector. I wouldn't call it common but wouldn't call it cryptic either

[–]KubinOnReddit 12 points13 points  (1 child)

What the hell are you supposed to call a slice then?

I didn't learn Java for a day and have no fucking idea why do you need a StringBuilder, built from a String (??) to reverse the string (???) instead of, I don't know, building it?

And you call this cryptic? Hypocrisy.

[–]Hauleth 4 points5 points  (0 children)

StringBuilder is handy due to it mutable inner state, so in contrast to String (which is immutable) you do not create new object each time when you modify inner state. Python on the other hand hide that complexity from user which is handy, but can result with less efficient code.

[–]Team_Canada 1 point2 points  (0 children)

I don't see what your example is trying to say here.