【Python】Code style used in Silicon Valley

English pages
スポンサーリンク

This is Yuki, a student engineer.
In this article, we will introduce the Silicon Valley style of code.

Code style is a way of writing source code in Python that is not error-prone, but is easy to understand for those who see it during collaborative development.

I think it is efficient to study Python while learning the style rules that are commonly known as Python’s code style.

スポンサーリンク

Code style used in Silicon Valley

Semicolons do not go to the next line

In Python, a semicolon at the end of a line like this does not cause an execution error, but it is difficult for the viewer to understand, so do not use it.

x = 2;
y = 3;

4 spaces for functions, if statements, etc.

Four spaces at the beginning of a function or if statement is considered good, so make it four.

def say_hello()
    print('hello')

Don’t write round brackets if you don’t need them.

Conditional expressions in the f statement can be round brackets, but they are not necessary and should not be extra round brackets to avoid confusion for the viewer.

if (x == y):
    print('It's same')

Operators are marked with a space.

x = 2

However, the function’s default argument, equals, does not have to be followed by a space.

def prac_func(x=2)
    print(x)

If you just want to connect two variables, do not use format.

word1 = 'hello'
word2 = 'goodbye'

new_word = word1 + word2

#new_word = '{}{}'.format(word1,word2)

When importing, separate one line at a time, not with a comma.

import os
import sys
import pathlib

#import os, sys, pathlib

Use global variables as little as possible, and capitalize them when used.

Global variables are useful but can break the system, so they should be written in uppercase for clarity.

GLOBAL_SCORE = 2

How to use import

First, write a standard Python module from the top.

Then write third-party packages, followed by your own modules.

Leave one line open for each.

import os
import sys #default

import flask #third package

import local_module #local package

import my_module #my package

Use corresponding alphabetic characters for variable names.

When defining variables, let’s use meaningful alphabets to prevent the code from getting too long and then not knowing what the variable is.

name = 'Yuki'

At the end

In this article, I explained the Silicon Valley style of code style.

Learning the code style known as the “high road” is an essential skill once you get a job.

There are some differences among companies, for example, whether it is better to use single or double quotation marks.

It is important to learn and be able to use a variety of grammars so that you can flexibly adapt to each company in addition to the Silicon Valley style of code.

ゆうき
Yuki

Thank you for reading!

コメント

タイトルとURLをコピーしました