Reputation: 25094
I am using sublime to automatically word-wrap python code-lines that go beyond 79 Characters as the Pep-8 defines. Initially i was doing return to not go beyond the limit.
The only downside with that is that anyone else not having the word-wrap active wouldn't have the limitation. So should i strive forward of actually word-wrapping or is the visual word-wrap ok?
Upvotes: 0
Views: 164
Reputation: 387557
PEP8 wants you to perform an actual word wrap. The point of PEP8’s stylistic rules is that the file looks the same in every editor, so you cannot rely on editor visualizations to satisfy PEP8.
This also makes you choose the point where to break deliberately. For example, Sublime will do a pretty basic job in wrapping that line; but you could do it in a more readable way, e.g.:
x = os.path.split(os.path.split(os.path.split(
os.path.split(os.path.split(path)[0])[0]
)[0])[0])
Of course that’s not necessarily pretty (I blame that mostly on this example code though), but it makes clear what belongs to what.
That being said, a good strategy is to simply avoid having to wrap lines. For example, you are using os.path.split
over and over; so you could change your import:
from os.path import split
x = split(split(split(split(split(path)[0])[0])[0])[0])
And of course, if you find yourself doing something over and over, maybe there’s a better way to do this, for example using Python 3.4’s pathlib
:
import pathlib
p = pathlib.Path(path).parents[2]
print(p.parent.absolute(), p.name)
Upvotes: 1
Reputation: 6596
In-file word wrapping would let your code conform to Pep-8 most consistently, even if other programmers are looking at your code using different coding environments. That seems to me to be the best solution to keeping to the standard, particularly if you are expecting that others will, at some point, be looking at your code.
If you are working with a set group of people on a project, or even in a company, it may be possible to coordinate with the other programmers to find what solution you are all most satisfied with.
For personal projects that you really aren't expecting anyone else to ever look at, I'm sure it's fine to use the visual word wrapping, but enforcing it yourself would certainly help to build on a good habit.
Upvotes: 0