Emerson Xu
Emerson Xu

Reputation: 438

How to indent if function call is too long in python2.x /3.x

I have a function with a very long parameter name, how can I do the indentation? E.g.,

ret = foo(
    first_argument=a,
    i_am_a_very_very_very_long_function_paramter_name=the_name_is_too_long_fit_in_80_chars_line)

I can't use \ to separate them as I do in IF conditions. The only way I can think of is to name the_name_is_too_long_fit_in_80_chars_line a shorter name like this:

b = the_name_is_too_long_fit_in_80_chars_line
ret = foo(
        first_argument=a,
        i_am_a_very_very_very_long_function_paramter_name=b)

Upvotes: 2

Views: 515

Answers (1)

hygull
hygull

Reputation: 8740

Emerson, you can create a list named args for positional arguments and a dictionary named kwargs for keyword arguments and use packing, unpacking concept of Python to send and receive arguments.

Please comment if this solution doesn't fulfill your need.

Below is a sample code that you can try:

def foo(*args, **kwargs):
    long_name = kwargs["long_name"]; # keyword argument (1st)
    v_long_name = kwargs["v_long_name"]; # keyword argument (2nd)
    fname = kwargs["fname"]; # keyword argument (3rd)
    a = kwargs["a"]; # keyword argument (4th)

    farg = args[0]; # positional argument (1st)
    sarg = args[1]; # positional argument (2nd)

    return fname

✓ Then prepare args and kwargs:

my_long_name = """Rogert Rendrick 
Jemen Cartel 
Neuron""";

firstname = "Rishikesh";
first_argument = 100;
second_argument = 200;

the_name_is_too_long_fit_in_80_chars_line = """80
lines of
...
setentences""";
the_second_name_is_too_long_fit_in_90_chars_line = """This
is
my
own
...
...
90
lines
"""

kwargs = {};
kwargs["long_name"] = my_long_name;
kwargs["v_long_name"] = 
the_name_is_too_long_fit_in_80_chars_line;
kwargs["fname"] = firstname;
kwargs ["a"] = the_second_name_is_too_long_fit_in_90_chars_line

args = [first_argument, second_argument]

✓ Finally, you can call using below syntax.

ret = foo(*args, **kwargs);
print(ret);  # Rishikesh

✓ The following calls are also valid.

ret = foo(12, *args, **kwargs)
ret = foo(*args, lastname="Agrawani", **kwargs)
ret = foo(67, 112, *args, lastname="Agrawani", **kwargs)

Upvotes: 1

Related Questions