Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Lensflare17550297dAnd people say Python is super readable, almost like pseudocode.
Meanwhile in Swift:
"Hello World".reversed() -
Root79778297dLike @Lensflare, I don’t understand the praise Python gets for readability — especially when its significant whitespace makes following blocks so bloody difficult. That, tuples, and its bizarre slicing syntax (among other things) just look so strange.
No, Python is kind of difficult to read, especially for a layman/nontechie.
Meanwhile in Ruby:
print “Hello World”.reverse -
lungdart3428297d
-
Lensflare17550297d@lungdart Then OP’s tip is a terrible one, especially for beginners.
Still, the slice syntax with the minus -1 has very low expressiveness and readability. -
Demolishun35074297d@Root you get used to it and at some point productivity goes way up. Then you do something else for a couple of years and you dump it all.
-
Lensflare17550296d
-
lungdart3428296d@Lensflare so your preferred language doesn't have reverse slice notation, but pythons syntax for it is bad?
reverse("Hello World")[2:4] is perfectly cromulent. Python has the extra feature of using a stride in a slice. And also has same defaults to reduce verbosity
[0:len(x)] == [0:-1] == [:] == [0:-1:1] == [::1] == [::]
Specifying a single index gives you a single item. Specifying a first and last item gives you a slice, and specifying a first last and stride gives you a slice that can skip elements (good for batching threads, or abusing it to reverse a list)
Worker1_jobs = jobs[::2] (even jobs)
Worker2_jobs = jobs[1::2] (odd jobs)
Worker1_jobs = jobs[::2]
Worker2_jobs = jobs[1::2] -
Lensflare17550296d@lungdart accessing an element, slicing, striding and reversing all with the same compact syntax and magic numbers (-1) doesn’t make it good. It’s still an unreadable cryptic mess.
I prefer to have separate, properly named and documented functions for that. -
lungdart3428296d@Lensflare be aware that using things like reverse() map() and filter() result in function calls and object creation and have a performance penalty vs doing advanced iteration patterns on the iterable. Not that this matters these days, especially Python.
-
Lensflare17550296d@lungdart Swift returns iterator-like collections for those kind of operations for that exact reason.
-
lungdart3428296d@Lensflare Python has that concept too, they call them generators, but creating them is not free, more like processed just in time.
-
Lensflare17550296d@lungdart nothing is free but a few function calls and memory allocations is constant time (and memory) and thus negligible in almost all of the use cases.
Easy Python Code for Beginner for reverse string
a = "Hello World"[ : :-1]
print(a)
random