Idioms and Anti-Idioms Question

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Jun 22 01:51:11 EDT 2009


On Mon, 22 Jun 2009 00:14:50 -0400, Ben Charrow wrote:

> I have a question about the "Using Backslash to Continue Statements" in
> the howto "Idioms and Anti-Idioms in Python"
> (http://docs.python.org/howto/doanddont.html#using-backslash-to-
continue-statements)
> 
> 
> It says:
> 
> "...if the code was:
> 
> value = foo.bar()['first'][0]*baz.quux(1, 2)[5:9] \
>          + calculate_number(10, 20)*forbulate(500, 360)
> 
> then it would just be subtly wrong."
> 
> What is subtly wrong about this piece of code?  I can't see any bugs and
> can't think of subtle gotchas (e.g. the '\' is removed or the lines
> become separated, because in both cases an IndentationError would be
> raised).


As examples go, it's pretty lousy because you can't just copy and paste 
it into an interpreter session and see for yourself. However, with some 
helper objects:


def forbulate(*args):
    return [1]

def calculate_number(*args):
    return 2

class K: pass

foo = K()
foo.bar = lambda: {'first': [1, 2, 3]}
baz = K()
baz.quux = lambda *args: [3]*10

value = foo.bar()['first'][0]*baz.quux(1, 2)[5:9] \
        + calculate_number(10, 20)*forbulate(500, 360)


I can run the example. I confirm that it works without a space after the 
line continuation character. Using Python 2.5, if I put a space after the 
backslash I get 

SyntaxError: unexpected character after line continuation character

followed by 

IndentationError: unexpected indent


So I don't understand the claim that the code is "subtly wrong" either. 
It looks to me like it's obviously wrong.



-- 
Steven



More information about the Python-list mailing list