Code Monkey home page Code Monkey logo

pystyleguide's Introduction

Python Code Style Guide

Principles

  1. PEP-8 Style Guide for Python Code
  2. Explicit is better than implicit
  3. Do not repeat yourself
  4. Keep things simple
  5. Keep things intuitive
  6. SOLID
  7. The Twelve-Factor App

Code

  1. Always prefer code maintainability and readability even if this might sacrifice some performance.
  2. Google Python Style Guide
  3. Keep functions and methods ideally less than 25 lines.
  4. Maintain methods lowest cyclomatic complexity: avoid if statements when possible (write branchless code), never use nested if. Be Never Nester.
  5. Don't concatenate strings with + it's unprofessional, error-prone, slower, and might be confusing, prefer f-string or "".join().
  6. The lesser indent your code has, the better.
  7. Use isort, and black to format your code.
  8. Use pylint, and mypy to check your code.

Method signature

  1. A method signature where the order of arguments "does not matter" (e.g. args are the same type) or the order is obvious is good for positional arguments:
def add(a: float, b: float) -> float: ...
  1. A method that expects non-interchangeable arguments is a good case to enforce keyword arguments, which will remove any ambiguity or wondering what should come first when calling the method:
def set_token(self, *, domain: str, token: str, expires_in: int) -> None: ...
  1. For a method around something you can combine positional and keyword arguments, using one positional for "something" and keyword arguments for everything else:
def get_instance(self, instance_id: int, *, include_inactive: Optional[bool] = False) -> T: ...

pystyleguide's People

Contributors

vsevolod-kolchinsky avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.