Home > Programming & scripting, Python > python statements are (or rather can be) messy

python statements are (or rather can be) messy

September 15th, 2007

I recently realized something about python that I wasn’t aware about. Python statements doesn’t force you to use whitespace before or after them.

When it comes to the = statement, this is rather obvious I guess… I sometimes write stuff like

foo="bar"

In other cases I had no idea it worked like this… for an example this is perfectly valid

print"hello"

In a language that uses indentation to block statements for nice readability, it feels kind of weird to allow this since it can make your code really really messy. How about these examples that are all perfectly valid:

"foo"if"bar"else"foobar"
for(x,y)in[(1,2),(3,4)]:print(x,y)
[(y)for(x,y)in[("foo",2),("bar",4)]if"foo"in(x)]

Simply forcing whitespace before and after statements in python would in my opinion be wise… even though you really REALLY shouldn’t write code like this anyways.

buffi Programming & scripting, Python

  1. September 20th, 2007 at 15:05 | #1

    Yes, exactly what python needs, another arbitrary formatting rule.

  2. September 21st, 2007 at 19:59 | #2

    I’m glad you agree.
    Also, I’m immune to irony.

  3. June 11th, 2008 at 04:32 | #3

    Your post would read better after this fix:

    s/statement/expression/g

  1. No trackbacks yet.