4

I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been more of a boon than a bane in writing projects.

However, coming from this land of brackets and parentheses and structs as far as the naked eye can see, I come across a small problem: I find Python difficult to read.

For example, the following block of text is hard for me to decipher unless I stare at it (which I dislike doing):

if foo:
   bar = baz
   while bar not biz:
      bar = i_am_going_to_find_you_biz_i_swear_on_my_life()

did_i_not_warn_you_biz()
my_father_is_avenged()

The problem occurs at the end of that if block: all the tabbing and then suddenly returning to a jarring block feels almost disturbing. As a solution, I've started coding my Python like this:

if foo:
   bar = baz
   while bar not biz:
      bar = i_am_going_to_find_you_biz_i_swear_on_my_life()
   #-- while --
#-- if --

did_i_not_warn_you_biz()
my_father_is_avenged()

And this, for some odd reason, makes me more able to read my own code. But I'm curious: has anyone else with my strange problem found easier ways to make their tabbed-out code more readable? I'd love to find out if there's a better way to do this before this becomes a huge habit for me.

7 Answers 7

24

Part of learning a new programming language is learning to read code in that language. A crutch like this may make it easier to read your own code, but it's going to impede the process of learning how to read anyone else's Python code. I really think you'd be better off getting rid of the end of block comments and getting used to normal Python.

15

I like to put blank lines around blocks to make control flow more obvious. For example:

if foo:
   bar = baz

   while bar not biz:
      bar = i_am_going_to_find_you_biz_i_swear_on_my_life()

did_i_not_warn_you_biz()
my_father_is_avenged()
8

You could try increasing the indent size, but in general I would just say, relax, it will come with time. I don't think trying to make Python look like C is a very good idea.

7

Rather than focusing on making your existing structures more readable, you should focus on making more logical structures. Make smaller blocks, try not to nest blocks excessively, make smaller functions, and try to think through your code flow more.

If you come to a point where you can't quickly determine the structure of your code, you should probably consider refactoring and adding some comments. Code flow should always be immediately apparent -- the more you have to think about it, the less maintainable your code becomes.

1
  • I've discovered that my Python code is a lot flatter than my C++ code. Don't know why. Jun 16, 2010 at 16:02
3

Perhaps the best thing would be to turn on "show whitespace" in your editor. Then you would have a visual indication of how far in each line is tabbed (usually a bunch of dots), and it will be more apparent when that changes.

1
  • If the indentation isn't obvious from the whitespace itself, then either it's not enough (four-column indents using spaces only, per PEP 8) or you're not reading it in a monospaced font. Either of those need to be fixed before deciding to make whitespace look like something else.
    – bignose
    May 3, 2009 at 14:02
0
from __future__ import braces

Need I say more? :)

Seriously, PEP 8, 'Blank lines', §4 is the official way to do it.

-1

I would look in to understanding more details about Python syntax. Often times if a piece of code looks odd, there usually is a better way to write it. For example, in the above example:

bar = foo if baz else None
while bar not biz:
    bar = i_am_going_to_find_you_biz_i_swear_on_my_life()

did_i_not_warn_you_biz()
my_father_is_avenged()

While it is a small change, it might help the readability. Also, in all honesty, I've never used a while loop, so there is a good change you would end up with a nice concise list comprehension or for loop instead. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.