문제

Coding style question: What is the recommended way of naming flag class attributes, i.e. attributes being True or False. Styles I can think of are:

  1. class MyClass: def my_method(self): self.request = False

  2. class MyClass: def my_method(self): self.is_request = False

  3. class MyClass: def my_method(self): self.request_flag = False

PEP8 does not seem to give a firm recommendation. Is there a canonical way of doing this?

도움이 되었습니까?

해결책

Considering that booleans are mostly used in coditions, the second way seems most appropriate.

o = MyClass()
...
if o.is_request: # very intuitive
    # it's a request
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top