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?
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?
I indent the subsequent lines 2 levels:
def method(self, alpha, beta, gamma, delta, epsilon, zeta, eta,
theta, iota, kappa):
pass
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).
If you have a function that takes so many variables as arguments, the last thing you have to worry about is indentation
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
)
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
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.
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)?
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
)?
If not, can you redefine the responsibilities of the parent object to include them?
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.