Pythonic way to write functions / methods with lots of arguments

Imagine the following:

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

The line overcomes 79 characters, so what is the pythonic way of its multi-line?

+10
source share
7 answers

I backtrack the following lines of level 2:

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

You can include line breaks in parentheses (or brackets), for example

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

(the number of spaces to include, of course, is up to you)

But in this case, you can 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).

+21
source

, "Pythonic" - , . , .

  • , ? , , (, )?

  • , ( self)?

  • , , ?

  • , , ? - - , .

+13

, , , , ,

+1

, 79 , :

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

, :

x.long_function_is_long(
    long_argument_is_loooooooooooooooooooooooooooooooooooooooong,
    longer_argument_is_looooooooooooooooooooooooooooooooooooooooonger
)
+1

, , :

    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())
0

, , , . , ( ):

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

, , , , *args

-2

All Articles