22

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?

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
Htechno
  • 5,901
  • 4
  • 27
  • 37
  • 6
    Consider grouping the parameters into cohesive data holding classes – manifest Jun 28 '10 at 21:17
  • For all, I know about *args, but I cannot group the arguments in this way, because I'm using a subset of Python that does not allow this syntax. – Htechno Jun 28 '10 at 22:32

9 Answers9

34

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 Z
  • 128,184
  • 27
  • 255
  • 279
  • Another +1 for `*args` and `**kwargs` – gomad Jun 28 '10 at 21:06
  • +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 Jun 28 '10 at 21:14
23

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
  • 68,372
  • 23
  • 116
  • 141
9

I indent the subsequent lines 2 levels:

def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta,
        theta, iota, kappa):
    pass
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 5
    +1 and accepted because just answered the question assuming I'm not idiot, and the solution is compatible with spaces and tabs indentation. – Htechno Jun 28 '10 at 22:38
  • This probably also answers the question on how to format *long* parameter and return type specifications like `def transpose_dict(dct: Mapping[T_a, Set[T_b]]) -> MutableMapping[T_b, Set[T_a]]:` using concepts from `collection.abc` usually takes a lot of space. – Wolf Aug 29 '23 at 10:31
2

I would write it like that

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

I think it looks nice and clean

Jan Rozycki
  • 1,603
  • 2
  • 15
  • 19
1

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
)
1

Another option would be to pass the parameters in a dict.

Michael Hodel
  • 2,845
  • 1
  • 5
  • 10
0

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

ooboo
  • 16,259
  • 13
  • 37
  • 32
0

For readability when I have long names I do this:

    file = load_sftp_file( SERVER_SFTP_HOST,
                           SERVER_SFTP_USER,
                           SERVER_SFTP_PASSWORD,
                           SERVER_SFTP_PRIVATE_KEY,
                           SERVER_SFTP_PATH,
                           FTP_DIR,
                           specific_filename=self.get_filename())
elad silver
  • 9,222
  • 4
  • 43
  • 67
-4

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

CastleDweller
  • 8,204
  • 13
  • 49
  • 69
  • 4
    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/. – Ben Hayden Jun 28 '10 at 21:33