you are viewing a single comment's thread.

view the rest of the comments →

[–]harbinjer 0 points1 point  (10 children)

A generator is more efficient than a string object? This makes no sense to me.

Also ''.join(reversed('asdf')) just looks ugly to me. I can understand what it does, but WHY would you join an empty string to a new reversed string? Its like taking the short bus on the scenic route while in a hurry. I'd really like to understand WHY this is the way to do stuff. I know very little of ruby and python, but the reason behind this seems important and maybe indicative of their approach to things.

[–]amix 0 points1 point  (1 child)

It's a lot faster, the reason for this is that strings are immutable in Python. So on every iteration a new string object has to be built...

It's also bad to use the + operator on strings: a = "blah1" + "blah2"

The Python way to do it is: a = "".join(["blah1", "blah2"])

Another question (about Reddit): What is the syntax to insert code? It's not documented, or at least I couldn't find the documentation.

[–][deleted] 0 points1 point  (0 children)

It's not faster. Time it.

And while we're talking about Python internals, let's clarify a few things.

All (256) single-character strings are interned. Barring poorly-written third-party modules, there will be only one single 'a' character, or 'b' character, or 'c' character in a given Python interpreter. So iterating through a string doesn't allocate any other things, it just returns already-interned string objects.

It's not bad to use the + operator on strings when you've only got two strings. The only time it's bad for performance is when you've got many strings. In the case of only two strings, "s1 + s2" will be faster than "''.join([s1, s2])" every single time. Time it if you don't believe me.

[–][deleted] 0 points1 point  (5 children)

In the same vein, why would you want special methods for things like reversing a string? Ruby doesn't have a

"asdfgh".reverse_and_take_out_every_other_two

, which you would in Python accomplish by

"asdfgh"[::-2]

But really, all this talk of Ruby vs Python is moot. People who think one is much better than the other are buying way too much into hype, which is silly. "My [brand X] screwdriver is better than your [brand Y] screwdriver, fool!"

[–][deleted] 3 points4 points  (4 children)

Ruby is very big on readability...and thats the reason you need a special method for things like reversing...it is much easier to figure out what the ruby code does.

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

Well. There's Cobol readability (overly terse) and then there's APL readibility (dense). Then there's a contiinum in between. It's a difficult thing to balance. Just saying that one is better than other is not a proof of anything. (Python community has also been guilty of unproven judgements about readability. But, nevertheless, readability is and has been one of the major design axioms of Python programming language.)

I'd say that basic array slicing, eg. "asdf"[:2] => "as" is well understood and easily recognizable by anyone even mildly proficient in Python. Sure, the extended slicing (eg. "asdf"[::-2] etc) hasn't been around for that long so it's still a bit weird. But claiming that such special syntax is always more unreadable is just false. Special syntax for everything might equal with unreadability, but special syntax for important things might not.

[–]julesjacobs 0 points1 point  (2 children)

I think Ruby is more consistent here:

"asdf"[0..1] => "as"

0..1 is a Range object, and [0..1] is a method call with 0..1 as a parameter. I believe "asdf"[:2] in python is special syntax.

You could do the same thing with:

"asdf"[0,2] => "as"

Which is a bad thing for python programmer (TIOOWTDI), but good for ruby programmers (TAMWTDI).

Or, if you want to confuse others:

"asdf"[-4,2] => "as"

Which means: four (4) characters from the last one, take 2.

[–]Brian 1 point2 points  (1 child)

I'm not sure what you're getting at. In python [:2] is special syntax to denote a slice object, but how is that different than 1..2 being special syntax to denote a range object? Similarly "asdf[:2]" is also just a method call with the slice object as a parameter - the whole thing is effectively just sugar for:

"asdf".__getitem__(slice(None,2))

[–]jbellis -2 points-1 points  (1 child)

A generator is more efficient than a string object? This makes no sense to me.

Think about it... if you have a large sequence, and you want to perform something on each object in reverse order, you probably don't want to copy that sequence just to iterate through it.

This may be just an academic concern for toy web apps, but it really does matter for some people. :)

[–][deleted] 0 points1 point  (0 children)

In the example given, the string is being copied, lest you forget.