Hi, I'm trying to figure out how to space following code according to pep-8:
try: import x
class A: def foo(self): # magic using x pass except ImportError: import y # different magic, using y
typical conditions are try/except and if/elif/else, though I can imagine a true hacker to wrap top-level definitions in with x, possibly even for/else, while/else as well ;-)
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.
option1: semantical, 2 lines before conditional top-levels
option2: legalist, 1 line before any indented top-level
pep8 tool only accepts option2
I think I would prefer option1; or explicitly leave it up user, then I can call option1 pep-8 compliant and someone else call option2 pep-8 compliant as well.
What do you think or prefer? Perhaps this was discussed ages ago and I can't find the traces?
Thanks, d.
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.
try: import x
class A: def foo(self): # magic using x pass
except ImportError: import y # different magic, using y
typical conditions are try/except and if/elif/else, though I can imagine a true hacker to wrap top-level definitions in with x, possibly even for/else, while/else as well ;-)
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.
option1: semantical, 2 lines before conditional top-levels
option2: legalist, 1 line before any indented top-level
pep8 tool only accepts option2
Which tool? Any configuration option that you could set to change the behaviour?
I think I would prefer option1; or explicitly leave it up user, then I can call option1 pep-8 compliant and someone else call option2 pep-8 compliant as well.
What do you think or prefer?
An irrelevance as far as I'm concerned. I know that others have different opinions so I'd better du...
Perhaps this was discussed ages ago and I can't find the traces?
Thanks, d.
I generally use something like (assuming either x or y will always exist) try: import x as value except ImportError: import y as value
class A: def foo(self): # magic using value
A real world example try: import Tkinter as tk ## Python 2.x except ImportError: import tkinter as tk ## Python 3.x
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:
try: import x except ImportError: import y as x
class A: # unconditional magic using x
Another variation:
try: from x import A except ImportError: import y
class A: # magic using y
...
I must admit I've never come across your variation, but if I did:
try: import x
class A: # why is this in the try block? pass
except ImportError: import y
class B: pass
...
[...]
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.