2

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)
k_ssb
  • 6,024
  • 23
  • 47
Emerson Xu
  • 428
  • 6
  • 13
  • 3
    Can you not rename the parameter? – user3483203 Jun 03 '18 at 23:08
  • 2
    best advice is to shorten the parameter. If you have a single parameter name more than a couple dozen characters, you're probably doing something wrong. – Bryan Oakley Jun 03 '18 at 23:10
  • 1
    Two thoughts: you can give things shorter names (this will likely lead to better code) and [A Foolish Consistency is the Hobgoblin of Little Minds](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds). No one is going to come to your house in the middle of the night if you write an 81 character line. If the "rules" are cramping your style and making your code worse you can just ignore them. – Patrick Haugh Jun 03 '18 at 23:12
  • Why are you having such a long variable? If you are looking for readability of code with descriptive variable name, then my friend you are doing it wrong. You should shorten the variable but still keeping it as summary of description. If required, describe the purpose of variable at the time of variable declaration in the form of comment – Moinuddin Quadri Jun 03 '18 at 23:12
  • Possible duplicate of [How to choose proper variable names for long names in python](https://stackoverflow.com/questions/48721582/how-to-choose-proper-variable-names-for-long-names-in-python) – Olivier Melançon Jun 04 '18 at 02:58

1 Answers1

1

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)
hygull
  • 8,464
  • 2
  • 43
  • 52