index.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <!DOCTYPE html>
  2. <html lang="en" class="h-full">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <meta name="color-scheme" content="light dark">
  7. <meta name="turbo-cache-control" content="no-cache" data-turbo-track="reload" data-track-token="4.1.0.825921057143">
  8. <!-- See retype.com -->
  9. <meta name="generator" content="Retype 4.1.0">
  10. <!-- Primary Meta Tags -->
  11. <title>Coding Style Guide | Portable Python 3.x Interpreter in Modern C</title>
  12. <meta name="title" content="Coding Style Guide | Portable Python 3.x Interpreter in Modern C">
  13. <meta name="description" content="Use four spaces for indentation. Do not use TAB.">
  14. <!-- Canonical -->
  15. <link rel="canonical" href="https://pocketpy.dev/coding-style-guide/">
  16. <!-- Open Graph -->
  17. <meta property="og:type" content="website">
  18. <meta property="og:url" content="https://pocketpy.dev/coding-style-guide/">
  19. <meta property="og:title" content="Coding Style Guide | Portable Python 3.x Interpreter in Modern C">
  20. <meta property="og:description" content="Use four spaces for indentation. Do not use TAB.">
  21. <meta property="og:site_name" content="pocketpy">
  22. <meta property="og:locale" content="en_US">
  23. <script data-cfasync="false">(function(){var cl=document.documentElement.classList,ls=localStorage.getItem("retype_scheme"),hd=cl.contains("dark"),hl=cl.contains("light"),wm=window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches;if(ls==="dark"||(!ls&&wm&&!hd&&!hl)){cl.remove("light");cl.add("dark")}else if(ls==="light"||(!ls&&!wm&&!hd&&!hl)){cl.remove("dark");cl.add("light")}})();</script>
  24. <link id="retype-favicon" rel="icon" href="../static/logo.png" />
  25. <link href="../resources/css/retype.css?v=4.1.0.825921057143" rel="stylesheet">
  26. <script data-cfasync="false" src="../resources/js/config.js?v=4.1.0.825921057143" data-turbo-eval="false" defer></script>
  27. <script data-cfasync="false" src="../resources/js/retype.js?v=4.1.0" data-turbo-eval="false" defer></script>
  28. <script id="lunr-js" data-cfasync="false" src="../resources/js/lunr.js?v=4.1.0.825921057143" data-turbo-eval="false" defer></script>
  29. <script id="prism-js" data-cfasync="false" src="../resources/js/prism.js?v=4.1.0.825921057143" defer></script>
  30. </head>
  31. <body>
  32. <div id="retype-app" class="relative text-base antialiased text-base-text bg-base-bg font-body">
  33. <div class="absolute bottom-0 left-0" style="top: 5rem; right: 50%"></div>
  34. <header id="retype-header" class="sticky top-0 z-30 flex w-full h-16 bg-header-bg border-b border-header-border md:h-20">
  35. <div class="container relative flex items-center justify-between pr-6 grow md:justify-start">
  36. <!-- Mobile menu button skeleton -->
  37. <button v-cloak class="skeleton retype-mobile-menu-button flex items-center justify-center shrink-0 overflow-hidden dark:text-white focus:outline-hidden rounded-full w-10 h-10 ml-3.5 md:hidden"><svg xmlns="http://www.w3.org/2000/svg" class="mb-px shrink-0" width="24" height="24" viewBox="0 0 24 24" role="presentation" style="margin-bottom: 0px;"><g fill="currentColor"><path d="M2 4h20v2H2zM2 11h20v2H2zM2 18h20v2H2z"></path></g></svg></button>
  38. <div v-cloak id="retype-sidebar-left-toggle-button"></div>
  39. <!-- Logo -->
  40. <div class="flex items-center justify-between h-full py-2 md:w-75">
  41. <div class="flex items-center px-2 md:px-6">
  42. <a id="retype-branding-logo" href="../" class="flex items-center leading-snug text-2xl">
  43. <span class="w-10 mr-2 grow-0 shrink-0 overflow-hidden">
  44. <img class="max-h-10 dark:hidden md:inline-block" src="../static/logo.png">
  45. <img class="max-h-10 hidden dark:inline-block" src="../static/logo.png">
  46. </span>
  47. <span class="dark:text-white font-bold line-clamp-1 md:line-clamp-2">pocketpy</span>
  48. </a><span id="retype-branding-label" class="inline-flex mt-1 px-2 py-1 ml-4 text-xs font-medium leading-none items-center rounded-md bg-branding-label-bg text-branding-label-text ring-1 ring-branding-label-border ring-inset md:inline-block">v2.1.8</span>
  49. </div>
  50. <span class="hidden h-8 border-r md:inline-block border-base-border"></span>
  51. </div>
  52. <div class="flex justify-between md:grow">
  53. <!-- Top Nav -->
  54. <nav id="retype-header-nav" class="hidden md:flex">
  55. <ul class="flex flex-col mb-4 md:pl-16 md:mb-0 md:flex-row md:items-center">
  56. <li class="mr-6">
  57. <a class="py-2 md:mb-0 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="">
  58. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  59. <g fill="currentColor">
  60. <path d="M11.03 2.59a1.501 1.501 0 0 1 1.94 0l7.5 6.363a1.5 1.5 0 0 1 .53 1.144V19.5a1.5 1.5 0 0 1-1.5 1.5h-5.75a.75.75 0 0 1-.75-.75V14h-2v6.25a.75.75 0 0 1-.75.75H4.5A1.5 1.5 0 0 1 3 19.5v-9.403c0-.44.194-.859.53-1.144ZM12 3.734l-7.5 6.363V19.5h5v-6.25a.75.75 0 0 1 .75-.75h3.5a.75.75 0 0 1 .75.75v6.25h5v-9.403Z"/>
  61. </g>
  62. </svg>
  63. <span>Home</span>
  64. </a>
  65. </li>
  66. <li class="mr-6">
  67. <a class="py-2 md:mb-0 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://pocketpy.dev/static/web/" target="_blank">
  68. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  69. <g fill="currentColor">
  70. <path d="M9.5 15.584V8.416a.5.5 0 0 1 .77-.42l5.576 3.583a.5.5 0 0 1 0 .842l-5.576 3.584a.5.5 0 0 1-.77-.42Z"/><path d="M1 12C1 5.925 5.925 1 12 1s11 4.925 11 11-4.925 11-11 11S1 18.075 1 12Zm11-9.5A9.5 9.5 0 0 0 2.5 12a9.5 9.5 0 0 0 9.5 9.5 9.5 9.5 0 0 0 9.5-9.5A9.5 9.5 0 0 0 12 2.5Z"/>
  71. </g>
  72. </svg>
  73. <span>Live Demo</span>
  74. </a>
  75. </li>
  76. <li class="mr-6">
  77. <a class="py-2 md:mb-0 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://pocketpy.github.io/examples/" target="_blank">
  78. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  79. <g fill="currentColor">
  80. <path d="M15.22 4.97a.75.75 0 0 1 1.06 0l6.5 6.5a.75.75 0 0 1 0 1.06l-6.5 6.5a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734L21.19 12l-5.97-5.97a.75.75 0 0 1 0-1.06Zm-6.44 0a.75.75 0 0 1 0 1.06L2.81 12l5.97 5.97a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215l-6.5-6.5a.75.75 0 0 1 0-1.06l6.5-6.5a.75.75 0 0 1 1.06 0Z"/>
  81. </g>
  82. </svg>
  83. <span>Live Examples</span>
  84. </a>
  85. </li>
  86. <li class="mr-6">
  87. <a class="py-2 md:mb-0 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://github.com/blueloveth/pocketpy" target="_blank">
  88. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  89. <g fill="currentColor">
  90. <path d="M10.303 16.652c-2.837-.344-4.835-2.385-4.835-5.028 0-1.074.387-2.235 1.031-3.008-.279-.709-.236-2.214.086-2.837.86-.107 2.02.344 2.708.967.816-.258 1.676-.386 2.728-.386 1.053 0 1.913.128 2.686.365.666-.602 1.848-1.053 2.708-.946.3.581.344 2.085.064 2.815.688.817 1.053 1.913 1.053 3.03 0 2.643-1.998 4.641-4.877 5.006.73.473 1.224 1.504 1.224 2.686v2.235c0 .644.537 1.01 1.182.752 3.889-1.483 6.94-5.372 6.94-10.185 0-6.081-4.942-11.044-11.022-11.044-6.081 0-10.98 4.963-10.98 11.044a10.84 10.84 0 0 0 7.112 10.206c.58.215 1.139-.172 1.139-.752v-1.719a2.768 2.768 0 0 1-1.032.215c-1.418 0-2.256-.773-2.857-2.213-.237-.58-.495-.924-.989-.988-.258-.022-.344-.129-.344-.258 0-.258.43-.451.86-.451.623 0 1.16.386 1.719 1.181.43.623.881.903 1.418.903.537 0 .881-.194 1.375-.688.365-.365.645-.687.903-.902Z"/>
  91. </g>
  92. </svg>
  93. <span>Github</span>
  94. </a>
  95. </li>
  96. <li class="mr-6">
  97. <a class="py-2 md:mb-0 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://discord.gg/WWaq72GzXv" target="_blank">
  98. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  99. <g fill="currentColor">
  100. <path d="M1.75 1h12.5c.966 0 1.75.784 1.75 1.75v9.5A1.75 1.75 0 0 1 14.25 14H8.061l-2.574 2.573A1.458 1.458 0 0 1 3 15.543V14H1.75A1.75 1.75 0 0 1 0 12.25v-9.5C0 1.784.784 1 1.75 1ZM1.5 2.75v9.5c0 .138.112.25.25.25h2a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h6.5a.25.25 0 0 0 .25-.25v-9.5a.25.25 0 0 0-.25-.25H1.75a.25.25 0 0 0-.25.25Z"/><path d="M22.5 8.75a.25.25 0 0 0-.25-.25h-3.5a.75.75 0 0 1 0-1.5h3.5c.966 0 1.75.784 1.75 1.75v9.5A1.75 1.75 0 0 1 22.25 20H21v1.543a1.457 1.457 0 0 1-2.487 1.03L15.939 20H10.75A1.75 1.75 0 0 1 9 18.25v-1.465a.75.75 0 0 1 1.5 0v1.465c0 .138.112.25.25.25h5.5a.75.75 0 0 1 .53.22l2.72 2.72v-2.19a.75.75 0 0 1 .75-.75h2a.25.25 0 0 0 .25-.25v-9.5Z"/>
  101. </g>
  102. </svg>
  103. <span>Discord</span>
  104. </a>
  105. </li>
  106. </ul>
  107. </nav>
  108. <div v-cloak class="flex justify-end grow">
  109. <div id="retype-mobile-search-button"></div>
  110. <doc-search-desktop></doc-search-desktop>
  111. <doc-theme-switch class="lg:ml-2"></doc-theme-switch>
  112. <doc-history></doc-history>
  113. </div>
  114. </div>
  115. </div>
  116. </header>
  117. <div id="retype-container" class="container relative flex bg-white">
  118. <!-- Sidebar Skeleton -->
  119. <div v-cloak class="fixed flex flex-col shrink-0 duration-300 ease-in-out bg-sidebar-left-bg border-sidebar-left-border sidebar top-20 w-75 border-r h-screen md:sticky transition-transform skeleton">
  120. <div class="flex items-center h-16 px-6">
  121. <input id="retype-filter-input-mock" class="w-full h-10 pl-8 px-3 py-2 transition-colors duration-200 ease-linear bg-filter-bg border border-filter-border rounded-lg shadow-none md:text-sm hover:border-filter-border-hover focus:outline-hidden focus:border-filter-border-focus placeholder-filter-placeholder" type="text">
  122. </div>
  123. <div class="pl-6 mt-1 mb-4">
  124. <div class="w-32 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  125. <div class="w-48 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  126. <div class="w-40 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  127. <div class="w-32 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  128. <div class="w-48 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  129. <div class="w-40 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  130. </div>
  131. <div class="shrink-0 mt-auto bg-transparent dark:border-base-border">
  132. <a class="flex items-center justify-center flex-nowrap h-16 text-gray-350 dark:text-dark-400 hover:text-gray-600 dark:hover:text-dark-300 transition-colors duration-150 ease-in docs-powered-by" target="_blank" href="https://retype.com/" rel="noopener">
  133. <span class="text-xs whitespace-nowrap">Powered by</span>
  134. <svg xmlns="http://www.w3.org/2000/svg" class="ml-2" fill="currentColor" width="96" height="20" overflow="visible"><path d="M0 0v20h13.59V0H0zm11.15 17.54H2.44V2.46h8.71v15.08zM15.8 20h2.44V4.67L15.8 2.22zM20.45 6.89V20h2.44V9.34z"/><g><path d="M40.16 8.44c0 1.49-.59 2.45-1.75 2.88l2.34 3.32h-2.53l-2.04-2.96h-1.43v2.96h-2.06V5.36h3.5c1.43 0 2.46.24 3.07.73s.9 1.27.9 2.35zm-2.48 1.1c.26-.23.38-.59.38-1.09 0-.5-.13-.84-.4-1.03s-.73-.28-1.39-.28h-1.54v2.75h1.5c.72 0 1.2-.12 1.45-.35zM51.56 5.36V7.2h-4.59v1.91h4.13v1.76h-4.13v1.92h4.74v1.83h-6.79V5.36h6.64zM60.09 7.15v7.48h-2.06V7.15h-2.61V5.36h7.28v1.79h-2.61zM70.81 14.64h-2.06v-3.66l-3.19-5.61h2.23l1.99 3.45 1.99-3.45H74l-3.19 5.61v3.66zM83.99 6.19c.65.55.97 1.4.97 2.55s-.33 1.98-1 2.51-1.68.8-3.04.8h-1.23v2.59h-2.06V5.36h3.26c1.42 0 2.45.28 3.1.83zm-1.51 3.65c.25-.28.37-.69.37-1.22s-.16-.92-.48-1.14c-.32-.23-.82-.34-1.5-.34H79.7v3.12h1.38c.68 0 1.15-.14 1.4-.42zM95.85 5.36V7.2h-4.59v1.91h4.13v1.76h-4.13v1.92H96v1.83h-6.79V5.36h6.64z"/></g></svg>
  135. </a>
  136. </div>
  137. </div>
  138. <!-- Sidebar component -->
  139. <doc-sidebar v-cloak>
  140. <template #sidebar-footer>
  141. <div class="shrink-0 mt-auto border-t md:bg-transparent md:border-none dark:border-base-border">
  142. <div class="py-3 px-6 md:hidden border-b dark:border-base-border">
  143. <nav>
  144. <ul class="flex flex-wrap justify-center items-center">
  145. <li class="mr-6">
  146. <a class="block py-1 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="">
  147. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  148. <g fill="currentColor">
  149. <path d="M11.03 2.59a1.501 1.501 0 0 1 1.94 0l7.5 6.363a1.5 1.5 0 0 1 .53 1.144V19.5a1.5 1.5 0 0 1-1.5 1.5h-5.75a.75.75 0 0 1-.75-.75V14h-2v6.25a.75.75 0 0 1-.75.75H4.5A1.5 1.5 0 0 1 3 19.5v-9.403c0-.44.194-.859.53-1.144ZM12 3.734l-7.5 6.363V19.5h5v-6.25a.75.75 0 0 1 .75-.75h3.5a.75.75 0 0 1 .75.75v6.25h5v-9.403Z"/>
  150. </g>
  151. </svg>
  152. <span>Home</span>
  153. </a>
  154. </li>
  155. <li class="mr-6">
  156. <a class="block py-1 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://pocketpy.dev/static/web/" target="_blank">
  157. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  158. <g fill="currentColor">
  159. <path d="M9.5 15.584V8.416a.5.5 0 0 1 .77-.42l5.576 3.583a.5.5 0 0 1 0 .842l-5.576 3.584a.5.5 0 0 1-.77-.42Z"/><path d="M1 12C1 5.925 5.925 1 12 1s11 4.925 11 11-4.925 11-11 11S1 18.075 1 12Zm11-9.5A9.5 9.5 0 0 0 2.5 12a9.5 9.5 0 0 0 9.5 9.5 9.5 9.5 0 0 0 9.5-9.5A9.5 9.5 0 0 0 12 2.5Z"/>
  160. </g>
  161. </svg>
  162. <span>Live Demo</span>
  163. </a>
  164. </li>
  165. <li class="mr-6">
  166. <a class="block py-1 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://pocketpy.github.io/examples/" target="_blank">
  167. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  168. <g fill="currentColor">
  169. <path d="M15.22 4.97a.75.75 0 0 1 1.06 0l6.5 6.5a.75.75 0 0 1 0 1.06l-6.5 6.5a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734L21.19 12l-5.97-5.97a.75.75 0 0 1 0-1.06Zm-6.44 0a.75.75 0 0 1 0 1.06L2.81 12l5.97 5.97a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215l-6.5-6.5a.75.75 0 0 1 0-1.06l6.5-6.5a.75.75 0 0 1 1.06 0Z"/>
  170. </g>
  171. </svg>
  172. <span>Live Examples</span>
  173. </a>
  174. </li>
  175. <li class="mr-6">
  176. <a class="block py-1 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://github.com/blueloveth/pocketpy" target="_blank">
  177. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  178. <g fill="currentColor">
  179. <path d="M10.303 16.652c-2.837-.344-4.835-2.385-4.835-5.028 0-1.074.387-2.235 1.031-3.008-.279-.709-.236-2.214.086-2.837.86-.107 2.02.344 2.708.967.816-.258 1.676-.386 2.728-.386 1.053 0 1.913.128 2.686.365.666-.602 1.848-1.053 2.708-.946.3.581.344 2.085.064 2.815.688.817 1.053 1.913 1.053 3.03 0 2.643-1.998 4.641-4.877 5.006.73.473 1.224 1.504 1.224 2.686v2.235c0 .644.537 1.01 1.182.752 3.889-1.483 6.94-5.372 6.94-10.185 0-6.081-4.942-11.044-11.022-11.044-6.081 0-10.98 4.963-10.98 11.044a10.84 10.84 0 0 0 7.112 10.206c.58.215 1.139-.172 1.139-.752v-1.719a2.768 2.768 0 0 1-1.032.215c-1.418 0-2.256-.773-2.857-2.213-.237-.58-.495-.924-.989-.988-.258-.022-.344-.129-.344-.258 0-.258.43-.451.86-.451.623 0 1.16.386 1.719 1.181.43.623.881.903 1.418.903.537 0 .881-.194 1.375-.688.365-.365.645-.687.903-.902Z"/>
  180. </g>
  181. </svg>
  182. <span>Github</span>
  183. </a>
  184. </li>
  185. <li class="mr-6">
  186. <a class="block py-1 inline-flex items-center text-sm whitespace-nowrap transition-colors duration-200 ease-linear md:text-header-text font-header-text hover:text-header-text-hover" href="https://discord.gg/WWaq72GzXv" target="_blank">
  187. <svg xmlns="http://www.w3.org/2000/svg" class="mb-px mr-1" width="18" height="18" viewBox="0 0 24 24" role="presentation">
  188. <g fill="currentColor">
  189. <path d="M1.75 1h12.5c.966 0 1.75.784 1.75 1.75v9.5A1.75 1.75 0 0 1 14.25 14H8.061l-2.574 2.573A1.458 1.458 0 0 1 3 15.543V14H1.75A1.75 1.75 0 0 1 0 12.25v-9.5C0 1.784.784 1 1.75 1ZM1.5 2.75v9.5c0 .138.112.25.25.25h2a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h6.5a.25.25 0 0 0 .25-.25v-9.5a.25.25 0 0 0-.25-.25H1.75a.25.25 0 0 0-.25.25Z"/><path d="M22.5 8.75a.25.25 0 0 0-.25-.25h-3.5a.75.75 0 0 1 0-1.5h3.5c.966 0 1.75.784 1.75 1.75v9.5A1.75 1.75 0 0 1 22.25 20H21v1.543a1.457 1.457 0 0 1-2.487 1.03L15.939 20H10.75A1.75 1.75 0 0 1 9 18.25v-1.465a.75.75 0 0 1 1.5 0v1.465c0 .138.112.25.25.25h5.5a.75.75 0 0 1 .53.22l2.72 2.72v-2.19a.75.75 0 0 1 .75-.75h2a.25.25 0 0 0 .25-.25v-9.5Z"/>
  190. </g>
  191. </svg>
  192. <span>Discord</span>
  193. </a>
  194. </li>
  195. </ul>
  196. </nav>
  197. </div>
  198. <a class="flex items-center justify-center flex-nowrap h-16 text-gray-350 dark:text-dark-400 hover:text-gray-600 dark:hover:text-dark-300 transition-colors duration-150 ease-in docs-powered-by" target="_blank" href="https://retype.com/" rel="noopener">
  199. <span class="text-xs whitespace-nowrap">Powered by</span>
  200. <svg xmlns="http://www.w3.org/2000/svg" class="ml-2" fill="currentColor" width="96" height="20" overflow="visible"><path d="M0 0v20h13.59V0H0zm11.15 17.54H2.44V2.46h8.71v15.08zM15.8 20h2.44V4.67L15.8 2.22zM20.45 6.89V20h2.44V9.34z"/><g><path d="M40.16 8.44c0 1.49-.59 2.45-1.75 2.88l2.34 3.32h-2.53l-2.04-2.96h-1.43v2.96h-2.06V5.36h3.5c1.43 0 2.46.24 3.07.73s.9 1.27.9 2.35zm-2.48 1.1c.26-.23.38-.59.38-1.09 0-.5-.13-.84-.4-1.03s-.73-.28-1.39-.28h-1.54v2.75h1.5c.72 0 1.2-.12 1.45-.35zM51.56 5.36V7.2h-4.59v1.91h4.13v1.76h-4.13v1.92h4.74v1.83h-6.79V5.36h6.64zM60.09 7.15v7.48h-2.06V7.15h-2.61V5.36h7.28v1.79h-2.61zM70.81 14.64h-2.06v-3.66l-3.19-5.61h2.23l1.99 3.45 1.99-3.45H74l-3.19 5.61v3.66zM83.99 6.19c.65.55.97 1.4.97 2.55s-.33 1.98-1 2.51-1.68.8-3.04.8h-1.23v2.59h-2.06V5.36h3.26c1.42 0 2.45.28 3.1.83zm-1.51 3.65c.25-.28.37-.69.37-1.22s-.16-.92-.48-1.14c-.32-.23-.82-.34-1.5-.34H79.7v3.12h1.38c.68 0 1.15-.14 1.4-.42zM95.85 5.36V7.2h-4.59v1.91h4.13v1.76h-4.13v1.92H96v1.83h-6.79V5.36h6.64z"/></g></svg>
  201. </a>
  202. </div>
  203. </template>
  204. </doc-sidebar>
  205. <div class="grow min-w-0 bg-body-bg">
  206. <!-- Render "toolbar" template here on api pages --><!-- Render page content -->
  207. <div class="flex">
  208. <div id="retype-main" class="min-w-0 p-4 grow md:px-16">
  209. <main class="relative pb-12 lg:pt-2">
  210. <div class="retype-markdown" id="retype-content">
  211. <!-- Rendered if sidebar right is enabled -->
  212. <div id="retype-sidebar-right-toggle"></div>
  213. <!-- Page content -->
  214. <doc-anchor-target id="coding-style-guide" class="break-words">
  215. <h1>
  216. <doc-anchor-trigger class="header-anchor-trigger" to="#coding-style-guide">#</doc-anchor-trigger>
  217. <span>Coding Style Guide</span>
  218. </h1>
  219. </doc-anchor-target>
  220. <doc-anchor-target id="indentation">
  221. <h2>
  222. <doc-anchor-trigger class="header-anchor-trigger" to="#indentation">#</doc-anchor-trigger>
  223. <span>Indentation</span>
  224. </h2>
  225. </doc-anchor-target>
  226. <p>Use four spaces for indentation. Do not use <code translate="no" v-pre>TAB</code>.</p>
  227. <doc-anchor-target id="strings">
  228. <h2>
  229. <doc-anchor-trigger class="header-anchor-trigger" to="#strings">#</doc-anchor-trigger>
  230. <span>Strings</span>
  231. </h2>
  232. </doc-anchor-target>
  233. <div id="strings-code-1" class="codeblock-wrapper"><doc-codeblock>
  234. <pre translate="no" class="language-python"><code v-pre class="language-python"># Prefer single quotes for strings
  235. s = 'this is a string'
  236. # Use double quotes only if the string itself contains a single quote
  237. s = &quot;this ' is single quote&quot;</code></pre>
  238. </doc-codeblock></div>
  239. <doc-anchor-target id="docstrings">
  240. <h2>
  241. <doc-anchor-trigger class="header-anchor-trigger" to="#docstrings">#</doc-anchor-trigger>
  242. <span>Docstrings</span>
  243. </h2>
  244. </doc-anchor-target>
  245. <p>Always use triple quotes for docstrings.</p>
  246. <div id="docstrings-code-1" class="codeblock-wrapper"><doc-codeblock>
  247. <pre translate="no" class="language-python"><code v-pre class="language-python">def f():
  248. &quot;&quot;&quot;This is a multi-line docstring.
  249. Here is some content. Docstrings partially support Markdown.
  250. &quot;&quot;&quot;
  251. def g():
  252. &quot;&quot;&quot;This is a single-line docstring.&quot;&quot;&quot;</code></pre>
  253. </doc-codeblock></div>
  254. <p>Use natural language to describe the function&#x27;s purpose. Do not enumerate each parameter and return value.</p>
  255. <div id="docstrings-code-2" class="codeblock-wrapper"><doc-codeblock>
  256. <pre translate="no" class="language-python"><code v-pre class="language-python"># Correct
  257. def add(a: int, b: int):
  258. &quot;&quot;&quot;Add two integers `a` and `b`.&quot;&quot;&quot;
  259. # Incorrect
  260. def add(a: int, b: int):
  261. &quot;&quot;&quot;
  262. @param a, the first argument
  263. @param b, the second argument
  264. @return, the result of a + b
  265. &quot;&quot;&quot;</code></pre>
  266. </doc-codeblock></div>
  267. <doc-anchor-target id="spaces">
  268. <h2>
  269. <doc-anchor-trigger class="header-anchor-trigger" to="#spaces">#</doc-anchor-trigger>
  270. <span>Spaces</span>
  271. </h2>
  272. </doc-anchor-target>
  273. <div id="spaces-code-1" class="codeblock-wrapper"><doc-codeblock>
  274. <pre translate="no" class="language-python"><code v-pre class="language-python"># Add a space after `,` or `:`
  275. a, b = 1, 2
  276. c = [1, 2, 3]
  277. d = {'key': 'value'}
  278. # Spaces may be added around operators
  279. res = 1 + 2
  280. if res &lt; 2: pass
  281. # Spaces can also be selectively added to indicate operator precedence
  282. x = x * 2 - 1
  283. hypot2 = x * x + y * y
  284. c = (a + b) * (a - b)
  285. # Add a space after `:` in type annotations
  286. def f(a: int, b: float): ...
  287. def g() -&gt; int: ...
  288. # Add spaces around `=` when specifying default values in function parameters
  289. def f(a: int = 1, b: int | None = None): ...
  290. # However, omit spaces if the parameter has no type annotation
  291. def f(a=1, b=2): pass
  292. # Do not add spaces in keyword arguments when calling functions
  293. print(1, 2, 3, end='', sep=',')
  294. f(a=10, b=20)</code></pre>
  295. </doc-codeblock></div>
  296. <doc-anchor-target id="naming-conventions">
  297. <h2>
  298. <doc-anchor-trigger class="header-anchor-trigger" to="#naming-conventions">#</doc-anchor-trigger>
  299. <span>Naming Conventions</span>
  300. </h2>
  301. </doc-anchor-target>
  302. <ul>
  303. <li>Classes: <code translate="no" v-pre>CapitalizedWords</code></li>
  304. <li>Functions and variables: <code translate="no" v-pre>lower_case_with_underscores</code></li>
  305. <li>Constants and enums: <code translate="no" v-pre>UPPER_CASE_WITH_UNDERSCORES</code> or <code translate="no" v-pre>CapitalizedWords</code></li>
  306. <li>Anonymous ordered variables: <code translate="no" v-pre>_0</code>, <code translate="no" v-pre>_1</code>, <code translate="no" v-pre>_2</code></li>
  307. <li>Discarded variables: <code translate="no" v-pre>_</code></li>
  308. <li>Some standard library functions: <code translate="no" v-pre>lowercase</code></li>
  309. </ul>
  310. <p>Here are some commonly used naming conventions:</p>
  311. <ul>
  312. <li><code translate="no" v-pre>self</code>: The first parameter of an instance method</li>
  313. <li><code translate="no" v-pre>cls</code>: The first parameter of class methods and <code translate="no" v-pre>__new__</code></li>
  314. </ul>
  315. <doc-anchor-target id="using-abbreviations">
  316. <h3>
  317. <doc-anchor-trigger class="header-anchor-trigger" to="#using-abbreviations">#</doc-anchor-trigger>
  318. <span>Using Abbreviations</span>
  319. </h3>
  320. </doc-anchor-target>
  321. <p>Use abbreviations only for temporary variables and internal implementations.</p>
  322. <p>Abbreviations should be well-established, include key syllables of the original word, and be immediately recognizable.</p>
  323. <ul>
  324. <li><code translate="no" v-pre>context</code> -&gt; <code translate="no" v-pre>ctx</code> (✔)</li>
  325. <li><code translate="no" v-pre>temporary</code> -&gt; <code translate="no" v-pre>tmp</code> (✔)</li>
  326. <li><code translate="no" v-pre>distribution</code> -&gt; <code translate="no" v-pre>dist</code> (✔)</li>
  327. <li><code translate="no" v-pre>visited</code> -&gt; <code translate="no" v-pre>vis</code> (<span class="docs-emoji">&#x274C;</span>)</li>
  328. </ul>
  329. <div id="using-abbreviations-code-1" class="codeblock-wrapper"><doc-codeblock>
  330. <pre translate="no" class="language-python"><code v-pre class="language-python"># Incorrect: Using abbreviations in public function parameters
  331. def some_pub_fn(ctx, req_id, data):
  332. pass
  333. # Correct
  334. def some_public_function(context, request_id, data):
  335. pass</code></pre>
  336. </doc-codeblock></div>
  337. <doc-anchor-target id="using-precise-terminology">
  338. <h3>
  339. <doc-anchor-trigger class="header-anchor-trigger" to="#using-precise-terminology">#</doc-anchor-trigger>
  340. <span>Using Precise Terminology</span>
  341. </h3>
  342. </doc-anchor-target>
  343. <p>Naming should convey precise meanings, especially when multiple synonyms exist.</p>
  344. <p>For example, <code translate="no" v-pre>count</code>, <code translate="no" v-pre>size</code>, and <code translate="no" v-pre>length</code> all relate to quantity, but they have different nuances:</p>
  345. <ul>
  346. <li><code translate="no" v-pre>count</code>: Represents a counted value</li>
  347. <li><code translate="no" v-pre>length</code>: Represents the number of elements in a container</li>
  348. <li><code translate="no" v-pre>size</code>: Represents the byte size of an object</li>
  349. </ul>
  350. <div id="using-precise-terminology-code-1" class="codeblock-wrapper"><doc-codeblock>
  351. <pre translate="no" class="language-python"><code v-pre class="language-python">s = 'aaabc⭐'
  352. count = s.count('a')
  353. length = len(s)
  354. size = len(s.encode())
  355. print(f&quot;{s!r} has a length of {length}, a size of {size} bytes, and contains {count} occurrences of 'a'&quot;)
  356. # 'aaabc⭐' has a length of 6, a size of 8 bytes, and contains 3 occurrences of 'a'</code></pre>
  357. </doc-codeblock></div>
  358. <doc-anchor-target id="using-professional-terminology">
  359. <h3>
  360. <doc-anchor-trigger class="header-anchor-trigger" to="#using-professional-terminology">#</doc-anchor-trigger>
  361. <span>Using Professional Terminology</span>
  362. </h3>
  363. </doc-anchor-target>
  364. <ul>
  365. <li>For item quantities in a game: <code translate="no" v-pre>quantity</code> is better than <code translate="no" v-pre>item_count</code></li>
  366. <li>For grid counts: <code translate="no" v-pre>area</code> (meaning surface area) is better than <code translate="no" v-pre>grid_count</code></li>
  367. </ul>
  368. <doc-anchor-target id="avoiding-built-in-names">
  369. <h3>
  370. <doc-anchor-trigger class="header-anchor-trigger" to="#avoiding-built-in-names">#</doc-anchor-trigger>
  371. <span>Avoiding Built-in Names</span>
  372. </h3>
  373. </doc-anchor-target>
  374. <div id="avoiding-built-in-names-code-1" class="codeblock-wrapper"><doc-codeblock>
  375. <pre translate="no" class="language-python"><code v-pre class="language-python"># Incorrect: Overwriting `builtins.map`
  376. map = [[1, 2, 3], [4, 5, 6]]
  377. # Incorrect: Overwriting `builtins.type`
  378. type = some_thing.type</code></pre>
  379. </doc-codeblock></div>
  380. <doc-anchor-target id="internal-functions-and-classes">
  381. <h3>
  382. <doc-anchor-trigger class="header-anchor-trigger" to="#internal-functions-and-classes">#</doc-anchor-trigger>
  383. <span>Internal Functions and Classes</span>
  384. </h3>
  385. </doc-anchor-target>
  386. <p>Use a single underscore <code translate="no" v-pre>_</code> as a prefix for internal functions. Never use a double underscore <code translate="no" v-pre>__</code> (except for magic methods).</p>
  387. <div id="internal-functions-and-classes-code-1" class="codeblock-wrapper"><doc-codeblock>
  388. <pre translate="no" class="language-python"><code v-pre class="language-python">def _internal_func():
  389. &quot;&quot;&quot;This is an internal function.&quot;&quot;&quot;
  390. class _InternalClass:
  391. def _internal_f(self): pass</code></pre>
  392. </doc-codeblock></div>
  393. <doc-anchor-target id="importing-modules">
  394. <h2>
  395. <doc-anchor-trigger class="header-anchor-trigger" to="#importing-modules">#</doc-anchor-trigger>
  396. <span>Importing Modules</span>
  397. </h2>
  398. </doc-anchor-target>
  399. <ol>
  400. <li>Import standard library modules first.</li>
  401. <li>Then import third-party dependencies.</li>
  402. <li>Finally, import project-specific modules.</li>
  403. </ol>
  404. <div id="importing-modules-code-1" class="codeblock-wrapper"><doc-codeblock>
  405. <pre translate="no" class="language-python"><code v-pre class="language-python">from typing import Any
  406. from collections import deque
  407. from array2d import array2d
  408. from ..utils import logger</code></pre>
  409. </doc-codeblock></div>
  410. <doc-anchor-target id="coding-practices">
  411. <h2>
  412. <doc-anchor-trigger class="header-anchor-trigger" to="#coding-practices">#</doc-anchor-trigger>
  413. <span>Coding Practices</span>
  414. </h2>
  415. </doc-anchor-target>
  416. <p>Use <code translate="no" v-pre>is not</code> when checking for <code translate="no" v-pre>None</code>. Do not explicitly compare with <code translate="no" v-pre>True</code> or <code translate="no" v-pre>False</code>.</p>
  417. <div id="coding-practices-code-1" class="codeblock-wrapper"><doc-codeblock>
  418. <pre translate="no" class="language-python"><code v-pre class="language-python"># Correct
  419. if x is not None: pass
  420. # Incorrect
  421. if x != None: pass
  422. # Correct
  423. x = True
  424. if x: pass
  425. if not x: pass
  426. # Incorrect
  427. if x == True: pass
  428. if x is True: pass
  429. if x != False: pass</code></pre>
  430. </doc-codeblock></div>
  431. <p>The <code translate="no" v-pre>if</code> statement implicitly calls <code translate="no" v-pre>bool()</code>, so it can be used to check if a container is empty.</p>
  432. <div id="coding-practices-code-2" class="codeblock-wrapper"><doc-codeblock>
  433. <pre translate="no" class="language-python"><code v-pre class="language-python">not_empty_list = [1]
  434. not_empty_string = '1'
  435. truth = True
  436. if not_empty_list:
  437. print('true value')
  438. if not_empty_string:
  439. print('true value')
  440. if truth:
  441. print('true value')
  442. # Explicitly checking for emptiness is also valid
  443. if len(not_empty_list) &gt; 0: pass</code></pre>
  444. </doc-codeblock></div>
  445. <doc-anchor-target id="references">
  446. <h2>
  447. <doc-anchor-trigger class="header-anchor-trigger" to="#references">#</doc-anchor-trigger>
  448. <span>References</span>
  449. </h2>
  450. </doc-anchor-target>
  451. <p><a href="https://peps.python.org/pep-0008/">PEP 8 – Style Guide for Python Code</a></p>
  452. <!-- Required only on API pages -->
  453. <doc-toolbar-member-filter-no-results></doc-toolbar-member-filter-no-results>
  454. </div>
  455. <footer id="retype-content-footer" class="clear-both">
  456. <nav id="retype-nextprev" class="print:hidden flex mt-14">
  457. <div class="w-1/2">
  458. <a class="px-5 py-4 h-full flex items-center break-normal font-medium text-body-link border border-base-border hover:border-base-border-hover rounded-l-lg transition-colors duration-150 relative hover:z-5" href="../gsoc2024/ideas/">
  459. <svg xmlns="http://www.w3.org/2000/svg" class="mr-3" width="24" height="24" viewBox="0 0 24 24" fill="currentColor" overflow="visible"><path d="M19 11H7.41l5.29-5.29a.996.996 0 10-1.41-1.41l-7 7a1 1 0 000 1.42l7 7a1.024 1.024 0 001.42-.01.996.996 0 000-1.41L7.41 13H19c.55 0 1-.45 1-1s-.45-1-1-1z" /><path fill="none" d="M0 0h24v24H0z" /></svg>
  460. <span>
  461. <span class="block text-xs font-normal text-base-text-muted">Previous</span>
  462. <span class="block mt-1">Project Ideas</span>
  463. </span>
  464. </a>
  465. </div>
  466. <div class="w-1/2">
  467. <a class="px-5 py-4 -mx-px h-full flex items-center justify-end break-normal font-medium text-body-link border border-base-border hover:border-base-border-hover rounded-r-lg transition-colors duration-150 relative hover:z-5" href="../performance/">
  468. <span>
  469. <span class="block text-xs font-normal text-right text-base-text-muted">Next</span>
  470. <span class="block mt-1">Performance</span>
  471. </span>
  472. <svg xmlns="http://www.w3.org/2000/svg" class="ml-3" width="24" height="24" viewBox="0 0 24 24" fill="currentColor" overflow="visible"><path d="M19.92 12.38a1 1 0 00-.22-1.09l-7-7a.996.996 0 10-1.41 1.41l5.3 5.3H5c-.55 0-1 .45-1 1s.45 1 1 1h11.59l-5.29 5.29a.996.996 0 000 1.41c.19.2.44.3.7.3s.51-.1.71-.29l7-7c.09-.09.16-.21.21-.33z" /><path fill="none" d="M0 0h24v24H0z" /></svg>
  473. </a>
  474. </div>
  475. </nav>
  476. </footer>
  477. </main>
  478. <div id="retype-page-footer" class="print:border-none border-t border-base-border pt-6 mb-8">
  479. <footer class="flex flex-wrap items-center justify-between print:justify-center">
  480. <div id="retype-footer-links" class="print:hidden">
  481. <ul class="flex flex-wrap items-center text-sm">
  482. </ul>
  483. </div>
  484. <div id="retype-copyright" class="print:justify-center py-2 text-footer-text font-footer-link text-sm leading-relaxed"><p>© Copyright 2026 - <a href="https://github.com/blueloveTH">blueloveTH</a> - All rights reserved.</p></div>
  485. </footer>
  486. </div>
  487. </div>
  488. <!-- Rendered if sidebar right is enabled -->
  489. <!-- Sidebar right skeleton-->
  490. <div v-cloak class="fixed top-0 bottom-0 right-0 translate-x-full bg-sidebar-right-bg border-sidebar-right-border lg:sticky lg:border-l lg:shrink-0 lg:pt-6 lg:translate-x-0 sm:w-1/2 lg:w-64 lg:z-0 md:w-104 sidebar-right skeleton">
  491. <div class="pl-5">
  492. <div class="w-32 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  493. <div class="w-48 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  494. <div class="w-40 h-3 mb-4 bg-skeleton-bg rounded-full loading"></div>
  495. </div>
  496. </div>
  497. <!-- User should be able to hide sidebar right -->
  498. <doc-sidebar-right v-cloak></doc-sidebar-right>
  499. </div>
  500. </div>
  501. </div>
  502. <doc-search-mobile></doc-search-mobile>
  503. <doc-back-to-top></doc-back-to-top>
  504. </div>
  505. <div id="retype-overlay-target"></div>
  506. <script data-cfasync="false">window.__DOCS__ = { "title": "Coding Style Guide", level: 1, icon: "file", hasPrism: true, hasMermaid: false, hasMath: false, tocDepth: 23 }</script>
  507. </body>
  508. </html>