by 16s · 14y ago · 0 comments
string s = "string";
reverse(s.begin(), s.end());
And here's how to reverse a string in Python:
s = "string"
print s[::-1]
In my opinion, the C++ version is far easier to read and just makes more sense. Edit - This is just one small example, however, I find it holds true for the entire languages in general. Also, I do a lot of Python and C++ systems programming so I use both frequently and while I prefer C++, I think Python is the best scripting language available today.
s = "string"
reversed(s)
It also has the plus of creating a generator, saving memory from a new array allocation.It's odd how Python has list.reverse() but not str.reverse(). Probably a conscious design decision sometime ago.
In this case, we are accessing the position "two colons minus one". I refuse to believe this makes any sort of sense for someone that has no deep knowledge of Python.
Your criticism is analogous to me looking at "int* foo = bar()" and saying, hey, we're multiplying a type by a name! How ridiculous!
using namespace boost::adaptors;
s | reversed
or reverse(s) ''.join(reversed(s))
It's still not the same as your example, because in C++ you reverse the string in-place. If you have a mutable array-like, you can do arrayish[:] = reversed(arrayish)
But to me the syntax with `s[::-1]` is already perfectly clear. There is the difference that `reversed` returns an iterator, while `s[::-1]` constructs a new string.In terms of readability, they both beg the question - is the reversed string ever actually held in memory at some point or can it occur lazily via latent reverse iteration?
I mean is the alleged readability of either language really answering any important questions?