I have this line of code:
assert 0 <= j <= self.n, "First edge needs to be between 0 and {}".format(self.n)
I want pep8 to be happy, but I don't understand how to break this line. I tried breaking after the comma and got invalid syntax. I've tried breaking the string with additional ""'s as in How to break long string lines for PEP8 compliance?. PEP8 was happy but the assert only produced the first half of the message.
What is the proper way to break long assert strings?
Use parens:
assert 0 <= j <= self.n, ("First edge needs to be "
"between 0 and {}".format(self.n))
Or:
assert 0 <= j <= self.n, ("First edge needs to be between 0 and {}"
.format(self.n))
Or use the parens of the format
function:
assert 0 <= j <= self.n, "First edge needs to be between 0 and {}".format(
self.n)
Considering assert
statements can be optimized away when you run the interpreter with the -O
option, you probably want to keep it a single statement and use string concatenation in parenthesis:
assert 0 <= j <= self.n, ('First edge needs to be between '
'0 and {}'.format(self.n))
or using f-strings in Python 3.6+:
assert 0 <= j <= self.n, ('First edge needs to be between '
f'0 and {self.n}')
If you don't care about optimization (e.g. you're writing tests), then breaking the line into two statements is also an option:
message = 'First edge needs to be between 0 and {}'.format(self.n)
assert 0 <= j <= self.n, message
You can force breaking to a new line like this:
assert 0 <= j <= self.n,\
"print stuff"
That always makes the line continue, if brackets and such aren't doing it automatically. And you can indent the next line to wherever makes it the most readable, as I did above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With