PyCharm이 알려줄 때 말을 잘 듣도록 하자...
"Python Enhancement Proposal 8"
Good
# 괄호로 정렬되는 경우
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 다른 요소와 구분을 위해 더 많은 들여쓰기를 한 경우
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# 매달린 형태의 들여쓰기는 하나의 들여쓰기 레벨을 추가해야함
foo = long_function_name(
var_one, var_two,
var_three, var_four)
Bad
# 수직정렬이 되지 않았을 경우, 첫번째 줄에는 인수가 없어야함
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 다른 요소와 구분이 되지 않는 경우
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
Good - if
# 들여쓰기 없는 경우
if (this_is_one_thing and
that_is_another_thing):
do_something()
# if문 내 코드와 구분하기 위한 주석 추가
if (this_is_one_thing and
that_is_another_thing):
# Since both conditions are true, we can frobnicate.
do_something()
# 추가적인 들여쓰기
if (this_is_one_thing
and that_is_another_thing):
do_something()
Good - 괄호
# 마지막 줄의 첫번째 요소에 위치
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
# 라인의 가장 앞에 위치
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
Good
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
class Calculator:
def __init__(self) -> None:
self.value: int = 0
def add(self, x: int, y: int) -> int:
return x + y
def subtract(self, x: int, y: int) -> int:
return x - y
def main() -> None:
calc = Calculator()
result1 = calc.add(5, 3)
print("Addition result:", result1)
result2 = calc.subtract(10, 4)
print("Subtraction result:", result2)
if __name__ == "__main__":
main()
"""This is the example module.
This module does stuff.
"""
from __future__ import barry_as_FLUFL
__all__ = ['a', 'b', 'c']
__version__ = '0.1'
__author__ = 'Cardinal Biggles'
import os
import sys
spam( ham[ 1 ], { eggs: 2 } )
=> spam(ham[1], {eggs: 2})
bar = (0, )
=> foo = (0,)
if x == 4 : print(x , y) ; x , y = y , x
=> if x == 4: print(x, y); x, y = y, x
i=i+1
=> i = i + 1
submitted +=1
=> submitted += 1
x = x * 2 - 1
=> x = x*2 - 1
hypot2 = x * x + y * y
=> hypot2 = x*x + y*y
c = (a + b) * (a - b)
=> c = (a+b) * (a-b)
# Good
def complex(real, imag=0.0):
return magic(r=real, i=imag)
# Bad
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
# argument annotation with a default value
# Good
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...
# Bad
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
# Google version
class Calculator:
"""
Contains various functions to perform common
mathematical operations between two numbers
Attributes:
prevSum (int): Stores value of previous operation
"""
def __init__(self):
"""
Initializes class attributes
Args:
No arguments
"""
self.prevSum = 0
def add(num1, num2):
"""
Calculate sum of two numbers
Args:
num1 (int): First Value
num2 (int): Second Value
Returns:
Sum of num1 and num2
"""
return num1 + num2
to be continue...