Python Forum
New to python, question regarding PEP8
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python, question regarding PEP8
#1
definitions = {
	"For loops": "For loops allow you to run a block of code repeatedly, just like while loops. However, for loops run "
	             "a block of code a set number of times.",
	"Functions": "A function is a block of code repeatedly, just like while loops. However, for loops run a block of"
	             "code a set number of times",
	"If statements": "An if statement runs a block of code based on whether or not a condition is true.",
	"Loops": "Loops check a condition and then run a code block. The loop will continue to check and run until a "
	         "specified condition is reached.",
	"Python": "Python is a programming language that's currently becoming more and more powerful with every new library"
	          " added to its collection."
}
Hey everyone, I'm going through this book by Eric Mathes, and I'm trying to develop good habits to use in the future. Currently, I'm working on dictionaries. I feel like I've formatted this simple code correctly, however, I receive an error from PyCharm stating, "PEP 8: E101 indentation contains mixed spaces and tabs".

Is this formatting okay? Or is something wrong with it so I can catch this habit and fix it? Thanks so much for your help!
Reply
#2
Formatting is okay.
It give warning because your dictionary values is a little long(uncommon) with split up text on new line.
Black is used much and have become more like standard way of formatting code in Python.
So eg Black will format it automatically like in code i show under.
definitions = {
    "For loops": "For loops allow you to run a block of code repeatedly, just like while loops. However, for loops run"
    "a block of code a set number of times.",
    "Functions": "A function is a block of code repeatedly, just like while loops. However, for loops run a block of"
    "code a set number of times",
    "If statements": "An if statement runs a block of code based on whether or not a condition is true.",
    "Loops": "Loops check a condition and then run a code block. The loop will continue to check and run until a "
    "specified condition is reached.",
    "Python": "Python is a programming language that's currently becoming more and more powerful with every new library"
    " added to its collection.",
}   
So a more common dict it will be format like this.
# Orginal
d = {"apple": "fruit", "ball": "object", "cricket": "sports", 'car': 'vehicles'}

# After Black
d = {
    "apple": "fruit",
    "ball": "object",
    "cricket": "sports",
    "car": "vehicles",
}
It's normal to have a formatter integrated in Editor,i have used Black for many years in VS Code.
For PyCharm look at Editor integration
Can also test online here Black Playground.
Reply
#3
Awesome, thanks for your help!
Reply
#4
I am not 100% sure the warning is about the format/indentation of the dict literal. On other hand if it was about other part of the code, you should get IndentationError when trying to run, not PyCharm/linter warning

In any case, never mix tabs and spaces for indentation. The PEP8 recommends using 4 spaces per level. Most IDEs have a setting to automatically convert tab to defined number of spaces.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob warning: trying to use pip to install pytest and pep8 in Command Prompt adifrank 4 5,345 Dec-20-2020, 04:23 AM
Last Post: adifrank
  Type hinting style and PEP8 hlovatt 2 2,865 May-07-2020, 08:12 PM
Last Post: hlovatt
  Visual Studio Code - PEP8 Lambda Issue Qui_Ten 1 2,735 Jan-28-2019, 08:17 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020