PEP8 compliance and exception messages ?

S

shearichard

Hi - PEP8 says lines should not exceed 79 characters in length
( http://www.python.org/dev/peps/pep-0008/ ).

So if you've got some code that looks like this :

raise fooMod.fooException("Some message which is quite long")

.... and assuming a certain amount of indenting you're going to break
that guideline.

However there's a way around that ! You can do this ...

raise fooMod.fooException("\
Some message \
which is quite long")

.... but the trouble is when that Exception is raised the message is
displayed as :

"Some message which is quite long"


I'm aware that a foolish consistency is the hobgoblin of something or
the other so maybe I should just let the PEP8 verifier complain but
otherwise does anyone have any ideas for how to get around this ?


Thanks

richard
 
C

Chris Rebert

Hi - PEP8 says lines should not exceed 79 characters in length
( http://www.python.org/dev/peps/pep-0008/ ).

So if you've got some code that looks like this :

raise fooMod.fooException("Some message which is quite long")

... and assuming a certain amount of indenting you're going to break
that guideline.

However there's a way around that ! You can do this ...

raise fooMod.fooException("\
       Some message \
       which is quite long")

... but the trouble is when that Exception is raised the message is
displayed as :

"Some message                     which is quite long"


I'm aware that a foolish consistency is the hobgoblin of something or
the other so maybe I should just let the PEP8 verifier complain but
otherwise does anyone have any ideas for how to get around this ?

Use implicit string literal concatenation:

raise fooMod.fooException(
"Some message "
"which is quite long")
#) # you could also put the closing paren here instead

Alternatively, you could disregard PEP 8 on this point on the grounds
that the 79/80 characters per line limit is outdated.

Cheers,
Chris
 
M

MRAB

Hi - PEP8 says lines should not exceed 79 characters in length
( http://www.python.org/dev/peps/pep-0008/ ).

So if you've got some code that looks like this :

raise fooMod.fooException("Some message which is quite long")

... and assuming a certain amount of indenting you're going to break
that guideline.

However there's a way around that ! You can do this ...

raise fooMod.fooException("\
Some message \
which is quite long")

... but the trouble is when that Exception is raised the message is
displayed as :

"Some message which is quite long"


I'm aware that a foolish consistency is the hobgoblin of something or
the other so maybe I should just let the PEP8 verifier complain but
otherwise does anyone have any ideas for how to get around this ?
You can use implied string concatenation:
'abcdef'

so:

raise fooMod.fooException(
"Some message "
"which is quite long")
 
A

Andreas Waldenburger

Hi - PEP8 says lines should not exceed 79 characters in length
( http://www.python.org/dev/peps/pep-0008/ ).

So if you've got some code that looks like this :

raise fooMod.fooException("Some message which is quite long")

... and assuming a certain amount of indenting you're going to break
that guideline.

[etc.]

Use implicit string literal concatenation:

[...]
But isn't explicit string literal concatenation better than implicit string literal concatenation?

.... sorry ...

Alternatively, you could disregard PEP 8 on this point on the grounds
that the 79/80 characters per line limit is outdated.
Maybe, but it's not outmoded.

/W
 
T

Tim Harig

Hi - PEP8 says lines should not exceed 79 characters in length
( http://www.python.org/dev/peps/pep-0008/ ).

So if you've got some code that looks like this :

raise fooMod.fooException("Some message which is quite long")

... and assuming a certain amount of indenting you're going to break
that guideline.

[etc.]

Use implicit string literal concatenation:

[...]
But isn't explicit string literal concatenation better than implicit
string literal concatenation?

So add the "+", it really doesn't change it much.
Maybe, but it's not outmoded.

I would say that it is not even outdated. Just because you happen to enjoy
longer lines doesn't mean that everybody does. Not all progammers have
20/10 vision and even those who have hardware to handled it don't
necessarily like having a single piece of code take up their entire display
just to be readable. Many of us like the extra ability that wider screen
technology gives us to actually be able to view more of the file at once
by splitting the display into a couple of columns.
 
S

Steven D'Aprano

So add the "+", it really doesn't change it much.

Perhaps not *much*, but it *may* change it a bit.

Implicit concatenation of literals is promised to be handled by the
compiler, at compile time:
1 0 LOAD_CONST 0 ('helloworld')
3 STORE_NAME 0 (s)
6 LOAD_CONST 1 (None)
9 RETURN_VALUE

This holds all the way back to Python 1.5 and probably older.

But explicit concatenation may occur at run-time, depending on the
implementation and the presence or absence of a keyhole optimizer. E.g.
in Python 2.4:
1 0 LOAD_CONST 0 ('hello')
3 LOAD_CONST 1 ('world')
6 BINARY_ADD
7 STORE_NAME 0 (s)
10 LOAD_CONST 2 (None)
13 RETURN_VALUE



A small difference, but a real one.
 
A

Andreas Waldenburger

Hi - PEP8 says lines should not exceed 79 characters in length
( http://www.python.org/dev/peps/pep-0008/ ).

So if you've got some code that looks like this :

raise fooMod.fooException("Some message which is quite long")

... and assuming a certain amount of indenting you're going to
break that guideline.

[etc.]

[...]
Alternatively, you could disregard PEP 8 on this point on the
grounds that the 79/80 characters per line limit is outdated.
Maybe, but it's not outmoded.
As a more useful (I hope) reply, my opinion in this case is to just make the line a little longer. Even if you can't read it all at once, it is pretty obvious what comes next: The rest of the error message. There is no additional functionality hidden there, and you don't need to see it all at once to grasp the meaning of the code.

/W
 
S

shearichard

PEP 8 also says those names are poorly chosen. Better:

    raise foomod.FooException("Some message which is quite long")


Take advantage of the parsing of string literals and parenthesis:

    raise foomod.FooException(
        "Some message"
        " which is quite long")

and for the sake of my eyes, avoid camelCase.

OK you got me ! Thanks for pointing this out, I will take a look at
the relevant section
 
S

shearichard

Hi - PEP8 says lines should not exceed 79 characters in length
(http://www.python.org/dev/peps/pep-0008/).
So if you've got some code that looks like this :
raise fooMod.fooException("Some message which is quite long")
... and assuming a certain amount of indenting you're going to
break that guideline.
[etc.]
[...]
Alternatively, you could disregard PEP 8 on this point on the
grounds that the 79/80 characters per line limit is outdated.
Maybe, but it's not outmoded.

As a more useful (I hope) reply, my opinion in this case is to just make the line a little longer. Even if you can't read it all at once, it is pretty obvious what comes next: The rest of the error message. There is no additional functionality hidden there, and you don't need to see it all at once to grasp the meaning of the code.

/W

Thanks to everyone for their helpful replies.

Thanks for the pointers towards implicit (or explicit) string
concatenation - just what was needed.

I appreciate everyone has different opinions by I'm happy to try to
stick with 79 character lines for the meantime - largely for the 'may
have a wide screen but like to have lots of files open in slim
windows' reason.

regards

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top