Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define functions with too many arguments to abide by PEP8 standard

Tags:

python

pep8

I have defined a function with a long list of arguments. The total characters in definition is above 80 and doesn't abide by PEP8.

def my_function(argument_one, argument_two, argument_three, argument_four, argument_five): 

What can be the best approach to avoid horizontal scrolling?

like image 549
Sudip Kafle Avatar asked Jul 28 '14 03:07

Sudip Kafle


People also ask

How do you avoid too many arguments in python?

There are two techniques that can be used to reduce a functions' arguments. One of them is to refactor the function, making it smaller, consequently, reducing the arguments' number. The Extract Method technique can be use to achieve this goal.

What is the maximum number of arguments a function can take?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

How many arguments can you use in custom functions?

More than two arguments and statements can be used in a function.

What are function arguments used for?

In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable. , is called a unary function. A function of two or more variables is considered to have a domain consisting of ordered pairs or tuples of argument values.


1 Answers

An example is given in PEP 8:

class Rectangle(Blob):      def __init__(self, width, height,                  color='black', emphasis=None, highlight=0): 

So that is the official answer. Personally I detest this approach, in which continuation lines have leading whitespace that doesn't correspond to any real indentation level. My approach would be:

class Rectangle(Blob):      def __init__(         self, width, height,         color='black', emphasis=None, highlight=0     ): 

. . . or just let the line run over 80 characters.

like image 66
BrenBarn Avatar answered Nov 10 '22 04:11

BrenBarn