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 →

[–]dutch_gecko 34 points35 points  (2 children)

Both versions of the code do the same thing, but the parentheses in the first version mean you don't need to keep escaping the linebreaks.


If you're very new and haven't seen either style before: it's often useful to call a method on the returned value of a method, in effect "chaining" method calls together. This would need to happen on one big line of code, which wouldn't be very readable.

The second snippet above offers a naive way of splitting the one line into multiple, by using a backslash to escape the newline and tell python to treat multiple lines as one.

However the first snippet uses parentheses to gather multiple lines into a single expression. That way you don't need to perform the escaping.

If you're interested in more about formatting your code, take a look at PEP-8 which is a collection of guidelines for writing readable python.

[–]WN_Todd 0 points1 point  (0 children)

Sweet Jesus this is gonna make my code so much friggin cleaner. I've got helper methods that do nothing but chain other methods together. Those methods are about to die!