[{"data":1,"prerenderedAt":670},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-pep8-style-explained":3},{"id":4,"title":5,"body":6,"description":656,"difficulty":657,"extension":658,"framework":659,"frameworkSlug":30,"meta":660,"navigation":184,"order":50,"path":661,"qaPath":662,"seo":663,"stem":664,"subtopic":665,"topic":666,"topicSlug":667,"updated":668,"__hash__":669},"blog\u002Fblog\u002Fpython-pep8-style-explained.md","Python PEP 8 & Style Explained — Naming, Layout, and Writing Idiomatic Code",{"type":7,"value":8,"toc":646},"minimark",[9,14,18,22,25,128,139,143,155,215,222,226,241,301,308,312,319,441,460,464,483,546,549,553,564,597,600,604,642],[10,11,13],"h2",{"id":12},"python-pep-8-style-explained","Python PEP 8 & style, explained",[15,16,17],"p",{},"PEP 8 is the official style guide for Python code. It isn't about pedantry — consistent\nstyle makes code readable across a whole community, so anyone can drop into any project and\nfeel at home. Here are the rules that matter most, plus the modern tooling that applies them\nfor you.",[10,19,21],{"id":20},"naming-conventions","Naming conventions",[15,23,24],{},"Names carry meaning, and PEP 8 assigns a casing to each kind of name. Following them lets\nreaders tell a class from a constant at a glance.",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-python shiki shiki-themes github-light github-dark","module_name.py            # lowercase_with_underscores\nclass HttpClient:         # CapWords \u002F PascalCase for classes\n    MAX_RETRIES = 3       # UPPER_CASE for constants\n    def send_request(self):   # lowercase_with_underscores for functions\u002Fmethods\n        local_var = 1     # same for locals\n        self._internal = 2    # single leading underscore = \"non-public\"\n","python","",[33,34,35,48,65,81,96,111],"code",{"__ignoreMap":31},[36,37,40,44],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"sVt8B","module_name.py            ",[36,45,47],{"class":46},"sJ8bj","# lowercase_with_underscores\n",[36,49,51,55,59,62],{"class":38,"line":50},2,[36,52,54],{"class":53},"szBVR","class",[36,56,58],{"class":57},"sScJk"," HttpClient",[36,60,61],{"class":42},":         ",[36,63,64],{"class":46},"# CapWords \u002F PascalCase for classes\n",[36,66,68,72,75,78],{"class":38,"line":67},3,[36,69,71],{"class":70},"sj4cs","    MAX_RETRIES",[36,73,74],{"class":53}," =",[36,76,77],{"class":70}," 3",[36,79,80],{"class":46},"       # UPPER_CASE for constants\n",[36,82,84,87,90,93],{"class":38,"line":83},4,[36,85,86],{"class":53},"    def",[36,88,89],{"class":57}," send_request",[36,91,92],{"class":42},"(self):   ",[36,94,95],{"class":46},"# lowercase_with_underscores for functions\u002Fmethods\n",[36,97,99,102,105,108],{"class":38,"line":98},5,[36,100,101],{"class":42},"        local_var ",[36,103,104],{"class":53},"=",[36,106,107],{"class":70}," 1",[36,109,110],{"class":46},"     # same for locals\n",[36,112,114,117,120,122,125],{"class":38,"line":113},6,[36,115,116],{"class":70},"        self",[36,118,119],{"class":42},"._internal ",[36,121,104],{"class":53},[36,123,124],{"class":70}," 2",[36,126,127],{"class":46},"    # single leading underscore = \"non-public\"\n",[15,129,130,131,134,135,138],{},"A single trailing underscore avoids clashing with keywords (",[33,132,133],{},"class_","); a double leading\nunderscore triggers name mangling (",[33,136,137],{},"__x","), so reserve it for that specific purpose.",[10,140,142],{"id":141},"layout-indentation-and-line-length","Layout: indentation and line length",[15,144,145,146,150,151,154],{},"Use ",[147,148,149],"strong",{},"4 spaces"," per indent level, never tabs. Keep lines to ",[147,152,153],{},"79 characters"," (PEP 8's\nlimit; many teams relax to 88 or 99). Break long lines inside parentheses, not with\nbackslashes.",[26,156,158],{"className":28,"code":157,"language":30,"meta":31,"style":31},"# Preferred — implicit continuation inside brackets\nresult = some_function(argument_one, argument_two,\n                       argument_three, argument_four)\n\ntotal = (first_value\n         + second_value\n         + third_value)     # operators at the start of the line read better\n",[33,159,160,165,175,180,186,196,204],{"__ignoreMap":31},[36,161,162],{"class":38,"line":39},[36,163,164],{"class":46},"# Preferred — implicit continuation inside brackets\n",[36,166,167,170,172],{"class":38,"line":50},[36,168,169],{"class":42},"result ",[36,171,104],{"class":53},[36,173,174],{"class":42}," some_function(argument_one, argument_two,\n",[36,176,177],{"class":38,"line":67},[36,178,179],{"class":42},"                       argument_three, argument_four)\n",[36,181,182],{"class":38,"line":83},[36,183,185],{"emptyLinePlaceholder":184},true,"\n",[36,187,188,191,193],{"class":38,"line":98},[36,189,190],{"class":42},"total ",[36,192,104],{"class":53},[36,194,195],{"class":42}," (first_value\n",[36,197,198,201],{"class":38,"line":113},[36,199,200],{"class":53},"         +",[36,202,203],{"class":42}," second_value\n",[36,205,207,209,212],{"class":38,"line":206},7,[36,208,200],{"class":53},[36,210,211],{"class":42}," third_value)     ",[36,213,214],{"class":46},"# operators at the start of the line read better\n",[15,216,217,218,221],{},"Surround top-level functions and classes with ",[147,219,220],{},"two"," blank lines, and methods inside a\nclass with one.",[10,223,225],{"id":224},"imports","Imports",[15,227,228,229,232,233,236,237,240],{},"Put imports at the top of the file, one module per line, grouped: ",[147,230,231],{},"standard library",",\nthen ",[147,234,235],{},"third-party",", then ",[147,238,239],{},"local"," — with a blank line between groups.",[26,242,244],{"className":28,"code":243,"language":30,"meta":31,"style":31},"import os\nimport sys\n\nimport requests\nfrom flask import Flask\n\nfrom myapp.models import User\n",[33,245,246,254,261,265,272,285,289],{"__ignoreMap":31},[36,247,248,251],{"class":38,"line":39},[36,249,250],{"class":53},"import",[36,252,253],{"class":42}," os\n",[36,255,256,258],{"class":38,"line":50},[36,257,250],{"class":53},[36,259,260],{"class":42}," sys\n",[36,262,263],{"class":38,"line":67},[36,264,185],{"emptyLinePlaceholder":184},[36,266,267,269],{"class":38,"line":83},[36,268,250],{"class":53},[36,270,271],{"class":42}," requests\n",[36,273,274,277,280,282],{"class":38,"line":98},[36,275,276],{"class":53},"from",[36,278,279],{"class":42}," flask ",[36,281,250],{"class":53},[36,283,284],{"class":42}," Flask\n",[36,286,287],{"class":38,"line":113},[36,288,185],{"emptyLinePlaceholder":184},[36,290,291,293,296,298],{"class":38,"line":206},[36,292,276],{"class":53},[36,294,295],{"class":42}," myapp.models ",[36,297,250],{"class":53},[36,299,300],{"class":42}," User\n",[15,302,303,304,307],{},"Avoid wildcard imports (",[33,305,306],{},"from module import *",") — they pollute the namespace and hide where\nnames come from.",[10,309,311],{"id":310},"whitespace-rules","Whitespace rules",[15,313,314,315,318],{},"PEP 8 is specific about spacing. Spaces around binary operators and after commas; ",[147,316,317],{},"no","\nspace inside brackets or before a call's parenthesis.",[26,320,322],{"className":28,"code":321,"language":30,"meta":31,"style":31},"# Good\nx = (a + b) * c\nfunc(arg, other=1)\nitems[1:3]\n\n# Bad\nx=( a+b )*c\nfunc (arg , other = 1)\nitems[1 : 3]\n",[33,323,324,329,351,368,384,388,393,413,427],{"__ignoreMap":31},[36,325,326],{"class":38,"line":39},[36,327,328],{"class":46},"# Good\n",[36,330,331,334,336,339,342,345,348],{"class":38,"line":50},[36,332,333],{"class":42},"x ",[36,335,104],{"class":53},[36,337,338],{"class":42}," (a ",[36,340,341],{"class":53},"+",[36,343,344],{"class":42}," b) ",[36,346,347],{"class":53},"*",[36,349,350],{"class":42}," c\n",[36,352,353,356,360,362,365],{"class":38,"line":67},[36,354,355],{"class":42},"func(arg, ",[36,357,359],{"class":358},"s4XuR","other",[36,361,104],{"class":53},[36,363,364],{"class":70},"1",[36,366,367],{"class":42},")\n",[36,369,370,373,375,378,381],{"class":38,"line":83},[36,371,372],{"class":42},"items[",[36,374,364],{"class":70},[36,376,377],{"class":42},":",[36,379,380],{"class":70},"3",[36,382,383],{"class":42},"]\n",[36,385,386],{"class":38,"line":98},[36,387,185],{"emptyLinePlaceholder":184},[36,389,390],{"class":38,"line":113},[36,391,392],{"class":46},"# Bad\n",[36,394,395,398,400,403,405,408,410],{"class":38,"line":206},[36,396,397],{"class":42},"x",[36,399,104],{"class":53},[36,401,402],{"class":42},"( a",[36,404,341],{"class":53},[36,406,407],{"class":42},"b )",[36,409,347],{"class":53},[36,411,412],{"class":42},"c\n",[36,414,416,419,421,423,425],{"class":38,"line":415},8,[36,417,418],{"class":42},"func (arg , ",[36,420,359],{"class":358},[36,422,74],{"class":53},[36,424,107],{"class":70},[36,426,367],{"class":42},[36,428,430,432,434,437,439],{"class":38,"line":429},9,[36,431,372],{"class":42},[36,433,364],{"class":70},[36,435,436],{"class":42}," : ",[36,438,380],{"class":70},[36,440,383],{"class":42},[15,442,443,444,446,447,450,451,455,456,459],{},"One exception worth remembering: in a keyword argument or default, no spaces around ",[33,445,104],{},"\n(",[33,448,449],{},"other=1","), but in a normal assignment you ",[452,453,454],"em",{},"do"," use them (",[33,457,458],{},"x = 1",").",[10,461,463],{"id":462},"idiomatic-comparisons-and-checks","Idiomatic comparisons and checks",[15,465,466,467,470,471,474,475,478,479,482],{},"PEP 8 codifies several \"Pythonic\" habits: compare to ",[33,468,469],{},"None"," with ",[33,472,473],{},"is",", test emptiness via\ntruthiness, and use ",[33,476,477],{},"isinstance"," over ",[33,480,481],{},"type(...) ==",".",[26,484,486],{"className":28,"code":485,"language":30,"meta":31,"style":31},"if value is None: ...           # not == None\nif not items: ...               # not len(items) == 0\nif isinstance(x, int): ...      # not type(x) == int\n",[33,487,488,510,525],{"__ignoreMap":31},[36,489,490,493,496,498,501,504,507],{"class":38,"line":39},[36,491,492],{"class":53},"if",[36,494,495],{"class":42}," value ",[36,497,473],{"class":53},[36,499,500],{"class":70}," None",[36,502,503],{"class":42},": ",[36,505,506],{"class":70},"...",[36,508,509],{"class":46},"           # not == None\n",[36,511,512,514,517,520,522],{"class":38,"line":50},[36,513,492],{"class":53},[36,515,516],{"class":53}," not",[36,518,519],{"class":42}," items: ",[36,521,506],{"class":70},[36,523,524],{"class":46},"               # not len(items) == 0\n",[36,526,527,529,532,535,538,541,543],{"class":38,"line":67},[36,528,492],{"class":53},[36,530,531],{"class":70}," isinstance",[36,533,534],{"class":42},"(x, ",[36,536,537],{"class":70},"int",[36,539,540],{"class":42},"): ",[36,542,506],{"class":70},[36,544,545],{"class":46},"      # not type(x) == int\n",[15,547,548],{},"These read more clearly and handle subclasses and edge cases correctly.",[10,550,552],{"id":551},"let-tools-enforce-it","Let tools enforce it",[15,554,555,556,559,560,563],{},"You shouldn't hand-apply PEP 8 — automate it. ",[147,557,558],{},"black"," is an opinionated formatter that\nrewrites code to a consistent style; ",[147,561,562],{},"ruff"," is a fast linter (and formatter) that flags\nviolations and many bugs. Run them in CI and a pre-commit hook.",[26,565,569],{"className":566,"code":567,"language":568,"meta":31,"style":31},"language-bash shiki shiki-themes github-light github-dark","ruff check .        # lint\nruff format .       # or: black .  — auto-format\n","bash",[33,570,571,585],{"__ignoreMap":31},[36,572,573,575,579,582],{"class":38,"line":39},[36,574,562],{"class":57},[36,576,578],{"class":577},"sZZnC"," check",[36,580,581],{"class":577}," .",[36,583,584],{"class":46},"        # lint\n",[36,586,587,589,592,594],{"class":38,"line":50},[36,588,562],{"class":57},[36,590,591],{"class":577}," format",[36,593,581],{"class":577},[36,595,596],{"class":46},"       # or: black .  — auto-format\n",[15,598,599],{},"With formatting automated, style stops being a code-review topic entirely.",[10,601,603],{"id":602},"recap","Recap",[15,605,606,607,612,613,618,619,624,625,628,629,632,633,635,636,638,639,641],{},"PEP 8 standardises Python style so code is readable everywhere: ",[147,608,609],{},[33,610,611],{},"snake_case"," for\nfunctions\u002Fvariables\u002Fmodules, ",[147,614,615],{},[33,616,617],{},"PascalCase"," for classes, ",[147,620,621],{},[33,622,623],{},"UPPER_CASE"," for constants;\n",[147,626,627],{},"4-space"," indents and ~79–88 character lines with breaks inside brackets; imports grouped\nstdlib \u002F third-party \u002F local; consistent whitespace around operators but not inside\nbrackets; and idioms like ",[33,630,631],{},"is None",", truthiness checks, and ",[33,634,477],{},". Don't apply it by\nhand — let ",[147,637,558],{}," and ",[147,640,562],{}," enforce it automatically.",[643,644,645],"style",{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":31,"searchDepth":50,"depth":50,"links":647},[648,649,650,651,652,653,654,655],{"id":12,"depth":50,"text":13},{"id":20,"depth":50,"text":21},{"id":141,"depth":50,"text":142},{"id":224,"depth":50,"text":225},{"id":310,"depth":50,"text":311},{"id":462,"depth":50,"text":463},{"id":551,"depth":50,"text":552},{"id":602,"depth":50,"text":603},"What PEP 8 actually requires — naming conventions, indentation and line length, import ordering, whitespace rules — plus the tools (black, ruff) that enforce style so you never argue about it again.","easy","md","Python",{},"\u002Fblog\u002Fpython-pep8-style-explained","\u002Fpython\u002Fidioms\u002Fpep8-style",{"title":5,"description":656},"blog\u002Fpython-pep8-style-explained","PEP 8 & Style","Pythonic Idioms","idioms","2026-06-19","XEt8vVLlKhBE4F-kCCoy5wwQIT1q3v1jA3BUSzPwYaw",1782244093045]