반응형
Python의 Very Long If 문
저는 파이썬으로 매우 긴 if 문을 가지고 있습니다.그것을 여러 줄로 나누는 가장 좋은 방법은 무엇입니까?가장 읽기 쉬운/일반적인 것을 의미합니다.
PEP8에 따르면, 긴 줄은 괄호 안에 넣어야 합니다.괄호를 사용할 때는 백슬래시를 사용하지 않고 줄을 나눌 수 있습니다.또한 줄 바꿈을 부울 연산자 뒤에 놓아야 합니다.
또한, 파이코드 스타일과 같은 코드 스타일 검사를 사용하는 경우 다음 논리 행의 코드 블록 들여쓰기가 서로 달라야 합니다.
예:
if (abcdefghijklmnopqrstuvwxyz > some_other_long_identifier and
here_is_another_long_identifier != and_finally_another_long_name):
# ... your code here ...
pass
다음은 라인 길이 제한에 대한 PEP 8의 직접적인 예입니다.
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
언급URL : https://stackoverflow.com/questions/5253348/very-long-if-statement-in-python
반응형
'programing' 카테고리의 다른 글
Pandas/Python에서 열을 필터링하기 위해 loc을 사용하는 것과 대괄호만 사용하는 것의 차이점은 무엇입니까? (0) | 2023.07.17 |
---|---|
파이썬 인터프리터 셸에서 마지막 명령을 반복하는 방법은 무엇입니까? (0) | 2023.07.17 |
모듈로(%) 연산자는 파이썬에서 음수에서 어떻게 작동합니까? (0) | 2023.07.17 |
'com.repository' 유형의 빈을 정의합니다.구성의 '사용자 리포지토리' (0) | 2023.07.17 |
파이썬 요청의 응답을 읽으려면 어떻게 해야 합니까? (0) | 2023.07.17 |