0

I am new to Python and I couldn't find the answer for this precise question elsewhere. Let's say one is using a Python function with several inputs. Ideally, for readability, I would like to write code as

my_variable = my_function (arg1 = bla_bla_bla_1,
                           arg2 = bla_bla_bla_2,
                           arg3 = bla_bla_bla_3)

This is very easy on RStudio with, for example, just using Enter. I am using Python on Visual Studio Code but I can't find a way to do it. Ideally, it would look like this:

enter image description here

But of course such code won't run since Enter and Tab or Space will break it. Is there anyway to achieve this? I see that this is different than, let's say, code wrapping. But I don't know the name of this property/way of writing code. Thanks in advance!

rioV8
  • 24,506
  • 3
  • 32
  • 49
Raul Guarini Riva
  • 651
  • 1
  • 10
  • 20

2 Answers2

1

Using \ should do the trick like so:

a = [5,4,6,\
     4,5,6] 
0

Python is quite prescriptive about style. The spec is known as PEP-8 https://www.python.org/dev/peps/pep-0008/

You can install a “linter” in VSCode which will alert you to general style breaches. See https://code.visualstudio.com/docs/python/linting

For your specific question, I think this is the prescribed way.

variable = my_function(
    arg1 = bla_bla_bla_1,
    arg2 = bla_bla_bla_2,
    arg3 = bla_bla_bla_3,
)

You might disagree, but python doesn’t leave much room for your opinion on this . This can be frustrating at first, but when embraced allows you to focus on coding instead of style; and, crucially, a standard style ensures readability when code is shared by multiple python developers.

Tom Harvey
  • 3,681
  • 3
  • 19
  • 28
  • 1
    I am fine with conventions and I agree this is helpful! How do you actually get what you wrote? You press enter and adjusts with spaces? Thanks! – Raul Guarini Riva Jun 23 '20 at 21:39
  • Enter for a new line and tab to ident. You should be able to set VSCode such that when pressing tab it inserts 4 spaces instead of a tab character https://stackoverflow.com/questions/29972396/how-can-i-customize-the-tab-to-space-conversion-factor#29972553 Will answer how to setup your indentation – Tom Harvey Jun 23 '20 at 21:48