views:

220

answers:

6

Which is preferred

def method(self):

or

def method( self ):

With spaces in the parenthesis

+16  A: 

Check out PEP 8. It says to do the first one.

Pesto
+1  A: 

http://www.python.org/dev/peps/pep-0008/

Amber
+1  A: 

Python Style Guide

No space around the inside of function signatures. Occasionally I put space inside the parens of a function call if the arguments are particularly hairy, but never on the definition. I don't think any language makes a habit of that, actually.

Eevee
+9  A: 

The common reference for Python style is PEP8, see: http://www.python.org/dev/peps/pep-0008/

To answer your question specifically, this is under "Pet Peeves":

Avoid extraneous whitespace in the following situations:

  • Immediately inside parentheses, brackets or braces.

    • Yes: spam(ham[1], {eggs: 2})

    • No: spam( ham[ 1 ], { eggs: 2 } )

jgeewax
+2  A: 

There's PEP 8 the Python Style Guide:

http://www.python.org/dev/peps/pep-0008/

It suggests the former style, but note the introduction carefully.

I find the latter to be visual nails-on-chalkboard, personally.

Dana
A: 

I don't think you should blindly follow the PEP-8 style, like a vow upon a bible.

The PEP8 is intended for code that goes in the python standard library, i.e. the modules you can import just after you've installed Python, but they have very different development constraints than most of the software I write.

So I tend to think a lot about each "rule" (either from the PEP8, the pylint/pychecker ones, or the Google Style Guide) and see how it can apply to my code -- the pros and cons.

As for your question, people usually don't use spaces inside parentheses, although I've seen it in some code by coworkers that like it, IMHO it detracts a little from readability, but it's a minor issue.

Marco Mariani
From the first section of PEP 8: "But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply."
Colonel Sponsz