0

My code is automatically formatted by the formatter, and it adds blank lines that I do not want. This happens while using both autopep8 and black. This is my code:

dict_n = {}
def new_dash():
    new_list = input("do something ").split(",")
    for i in range(len(new_list)):
        # dict_n[new_list[i]] = [i]
        dict_n[i] = new_list[i]
    print(dict_n)
new_dash()

which gets formatted to:

dict_n = {}


def new_dash():
    new_list = input("do something ").split(",")
    for i in range(len(new_list)):
        # dict_n[new_list[i]] = [i]
        dict_n[i] = new_list[i]
    print(dict_n)


new_dash()

Is there a way I can prevent the extra lines being added automatically by the formatter, possibly by editing the settings.json file in VSCode?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Babul 7154
  • 61
  • 2
  • 9
  • 4
    probably not in Black (since it is designed to be minimally configurable). But the extra line is just PEP8 style convention: https://peps.python.org/pep-0008/#blank-lines – juanpa.arrivillaga Mar 21 '23 at 17:02
  • 5
    Some advice form someone who gets paid to work with other people's code: please don't. Just stick with the default black formatting because it makes _our_ job a lot easier when those newlines are there. You don't use a formatter to make things nice and concise, you use it to make sure everyone sees the same code, with enough compromises to always keep code easy to read. If you use something like black or autopep, just use the default configuration, because you're not using them "for you", you're using them for everyone who'll ever have to work with or look at your code. – Mike 'Pomax' Kamermans Mar 21 '23 at 17:10
  • 1
    ["Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting."](https://pypi.org/project/black/). The name (and logo) refers to Henry Ford's comment on the Model T: "‘Any customer can have a car painted any colour that he wants so long as it is black.’" – chepner Mar 21 '23 at 17:13
  • 3
    Code is read far more often than it is written. A little vertical white space makes the code much easier to read. That's a point supported by research. – Tim Roberts Mar 21 '23 at 17:14
  • In this case, you can have any number of lines between global definitions that you want, so long as it's 2. – chepner Mar 21 '23 at 17:14
  • 2
    Using a standard format also prevents your git-diffs (or whatever version-control you use) from having thousands of whitespace changes when someone else's autoformatter formats your code and commits it. Having worked with someone who refused to follow a standard format and insisted on randomly formatting everyone else's code to their own idiosyncratic standards, I can say from experience that it is NOT fun to have to wade through thousands of whitespace edits to find the few consequential changes in a commit – Pranav Hosangadi Mar 21 '23 at 17:16
  • Hello @Mike'Pomax'Kamermans sir, I am a college student . When I start debugging, I have to check every single variable and function so that my script run without any logical errors. Sometimes my code length goes up to 55-60 lines (pretty big for me and new learner) I have to scroll down to view codes that is annoying (because of extra new lines).And that's my problem. I just want compact view, no extra lines, no extra spaces only clean codes. – Babul 7154 Mar 21 '23 at 17:26
  • 3
    No, you don't. You use an editor that makes it easy to walk through your code, and you read error messages that tell you _exactly_ where things go wrong, and let you immediately jump to those lines in your editor. If you're not using a Python IDE with that functionality, or even something more general like VS Code with python plugins, start there. *Especially* if you're a student, use black, and don't mess with it. Your grade _will_ in part be based on whether your code is legible. That prof or TA is _not_ going to be in a good mood after reviewing 20 poorly written programs. – Mike 'Pomax' Kamermans Mar 21 '23 at 18:09
  • Thank you @Mike'Pomax'Kamermans for telling me the benefits of formatter – Babul 7154 Mar 22 '23 at 08:27

1 Answers1

-1

First of all, this is a formatting standard, for the sake of code specification and code readability, etc. Generally, it is not recommended to change it.

If you really want to change, the following is a solution, but it can only remove the blank lines above. To add the following settings in settings.json,

    "python.formatting.provider": "black",
    "python.formatting.blackArgs": [
        "--skip-source-first-line",
    ],

enter image description here

See more about this settings here.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • That option doesn't mean "stop adding blank lines above functions". It means "skip the first line of source code". – user2357112 Mar 22 '23 at 06:58