Programing

Python에서 매우 긴 If 문

crosscheck 2020. 9. 16. 07:08
반응형

Python에서 매우 긴 If 문 [중복]


이 질문에 이미 답변이 있습니다.

파이썬에 매우 긴 if 문이 있습니다. 여러 줄로 나누는 가장 좋은 방법은 무엇입니까? 기껏해야 가장 읽기 쉽고 일반적입니다.


PEP8 에 따르면 긴 줄은 괄호로 묶어야합니다. 괄호를 사용할 때 백 슬래시를 사용하지 않고 줄을 나눌 수 있습니다. 부울 연산자 뒤에 줄 바꿈을 넣어야합니다 .

또한 pycodestyle 과 같은 코드 스타일 검사를 사용하는 경우 다음 논리 줄은 코드 블록에 대해 다른 들여 쓰기를 가져야합니다.

예를 들면 :

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

반응형