[Python-ideas] pep8 clarification, conditional top-level class/function leading newlines
steve at pearwood.info
Sun Mar 3 01:01:31 CET 2013
On 03/03/13 01:06, Mark Lawrence wrote:
> On 02/03/2013 13:53, Dima Tisnek wrote:
> > Hi,
> > I'm trying to figure out how to space following code according to pep-8:
> I ignore PEP 8 whenever I feel like it, it's not written in stone, it's simply a guide.
Technically you don't, since PEP 8 states to break the rules when needed, so even when you break it you are obeying it :-)
> > try:
> > import x
> > class A:
> > def foo(self):
> > # magic using x
> > pass
> > except ImportError:
> > import y
> > # different magic, using y
Where possible, I would write that as:
import y as x
# unconditional magic using x
from x import A
# magic using y
I must admit I've never come across your variation, but if I did:
class A: # why is this in the try block?
> > PEP-8 states to separate top-level class and functions by 2 blank
> > lines and methods in a class by 1 blank line. This case falls into the
> > crack, it's neither strictly top-level, nor a sub-level.
Note that PEP 8 states "top level", not "global". That means, no leading indentation. So a conditional class definition falls into the "1 line between indented classes and function" bucket.
But frankly, I would make a final judgement only after actually typing up the code and looking at it.
More information about the Python-ideas