pythonhmmm
pythonhmmm

Reputation: 923

maximum line length in python

Should i still follow the rule of maximum line length of 79 char while coding in python.

if yes so when to use enter and \ for line breaking.

eg,

qy = MyModel.objects.filter(name='abcd', modified_on__range=
                                         (now-delta_6, now),type=1) \
                                         .select_related('Author')

I use emacs for my python coding. so when i use enter for line breacking sometime i can use tab for indentation and sometime i have to use \ for indentation

so what the best way of doing it.

Thanks

Upvotes: 1

Views: 4596

Answers (2)

Sven Marnach
Sven Marnach

Reputation: 601401

A maximum line length of 79 characters is recommended by PEP 8, and I usually don't find it too hard to follow this recommendation.

Also in accordance with PEP 8, I try to rely on the implied line continuation inside parentheses. The given example can be easily split into two statements:

qy = MyModel.objects.filter(
    name='abcd', modified_on__range=(now - delta_6, now),type=1)
qy = qy.select_related('Author')

If your code performs a lot of method chaining, you might prefer to add a pair of parens to get implicit line continuation:

qy = (MyModel.objects
      .filter(name='abcd', modified_on__range=(now - delta_6, now),type=1)
      .select_related('Author'))

This puts every chained method on a line of its own, making it easy to see the steps involved at a glance.

Upvotes: 7

Andrew Clark
Andrew Clark

Reputation: 208405

I would rewrite your code like this if you want to follow the PEP 8 guidelines:

qy = MyModel.objects.filter(name='abcd',
                            modified_on__range=(now-delta_6, now),
                            type=1).select_related('Author')

As for whether you should following the maximum line length suggested in PEP 8, that is really up to you.

PEP 8 is designed to improve readability of Python code, and the 79 character line length is for compatibility with certain width limited devices and for easy side-by-side code viewing. I have pretty wide screens so even though I try to follow PEP 8 most of the time, this is one rule that I will ignore if I feel splitting up the line decreases readability.

Upvotes: 2

Related Questions