programing

Python의 Very Long If 문

cafebook 2023. 7. 17. 21:24
반응형

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

반응형