Programmer-relevant whitespace in python
A comment was made at #rsparlyredux along the lines that while python's enforcement of proper indentation stops people from not indenting at all, it also stops them from adding extra indentation, and sometimes extra indentation can be useful for the programmer. IIRC the example was OpenGL, where a common pattern in C is:
gl_start(TRIANGLE); gl_add_vertex(0, 0, 0); gl_add_vertex(0, 1, 0); gl_add_vertex(0, 0, 1); gl_end();
If you tried to do this using python's standard syntax, it would complain that the indentation of the middle lines is unnecessary. However, you can actually do this in python, by turning the block of code into one logical code line - it's maybe 5% extra typing when you're dealing with this edge-case, but I consider that a fair trade for 10% less typing under normal circumstances:
gl_start(TRIANGLE); \ gl_add_vertex(0, 0, 0); \ gl_add_vertex(0, 1, 0); \ gl_add_vertex(0, 0, 1); \ gl_end()
(semicolon to separate multiple statements on one line, backslash to keep the whole thing as one logical line, so internal whitespace is ignored)
Edit: there is an even more pythonic syntax for this sort of thing. I'm not sure the stock GL library has been updated to support the with
statement yet, but in theory:
Edit edit: A lovely Rory had the same thought, and has actually written the code to make it work, see first comment /o/
with gl_start(TRIANGLE): gl_add_vertex(0, 0, 0) gl_add_vertex(0, 1, 0) gl_add_vertex(0, 0, 1)
(gl_end() is called automatically at the end of the `with` block)
2011-11-26 02:18:01 -0600