views:

164

answers:

6

Imagine this:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota, kappa):
    pass

The line overpass the 79 characters, so, what's the pythonic way to multiline it?

+1  A: 

I indent the subsequent lines 2 levels:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta,
        theta, iota, kappa):
    pass
Ignacio Vazquez-Abrams
+1 and accepted because just answered the question assuming I'm not idiot, and the solution is compatible with spaces and tabs indentation.
mkotechno
+11  A: 

You can include line breaks within parentheses (or brackets), e.g.

def method(self, alpha, beta, gamma, delta, epsilon, zeta,
                 eta, theta, iota, kappa):
    pass

(the amount of whitespace to include is, of course, up to you)

But in this case, you could also consider

def method(self, *args):
    pass

and/or

def method(self, **kwargs):
    pass

depending on how you use the arguments (and how you want the function to be called).

David Zaslavsky
+1 for `*args` and `**kwargs`
Felix Kling
Another +1 for `*args` and `**kwargs`
gomad
+1 multiple lines with parenthesis. It took me awhile to figure out you could do this and a lot of beginners are unaware of its convenience. Say goodbye to ugly \!
manifest
+1  A: 

If you have a function that takes so many variables as arguments, the last thing you have to worry about is indentation

ooboo
Not really an answer though, is it?
ire_and_curses
I want to up-vote this sooo badly, but yeah... Should have been a comment.
Shog9
A: 

I just split to the open bracket once 79 is hit, like this:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta, theta, iota,
           kappa):

And when names are too long to be put after the opening bracket, I do it like this:

x.long_function_is_long(
    long_argument_is_loooooooooooooooooooooooooooooooooooooooong,
    longer_argument_is_looooooooooooooooooooooooooooooooooooooooonger
)
Longpoke
A: 

I usually do this, I don't know if it's the best, but it works. Since I have to split the line I make them equally longer (if I can):

def method(self, alpha, beta, gamma, delta, epsilon, \
                 zeta, eta, theta, iota, kappa):
    pass

Also, having such quantity of parameters I would recommend the same as David, use *args

Oscar Carballal
The \ character is unnecessary. In Python, grouping with parentheses allows code to span multiple lines - this is pointed out in PEP 8 http://www.python.org/dev/peps/pep-0008/.
BenHayden
Didn't know about that. Thanks :)
Oscar Carballal
+4  A: 

I think the 'Pythonic' way of answering this is to look deeper than syntax. Passing in that many arguments to a method indicates a likely problem with your object model.

  1. First of all, do you really need to pass that many arguments to this method? Perhaps this is an indication that the work could be better done elsewhere (by an object that already has access to the variables)?

  2. If this really is the best place for the method, then could some of those arguments be supplied as instance variables of this object itself (via self)?

  3. If not, can you redefine the responsibilities of the parent object to include them?

  4. If not, can you encapsulate any of the individual arguments in a composite object that formalizes the relationship between them? If any of the arguments have anything in common, then this should be possible.

ire_and_curses