Python: assigning class attributes to boolean / flag

Question about coding style. What is the recommended way to assign the attributes of a flag class to a name, that is, Trueor attributes False. The styles I can think of are as follows:

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

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

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

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

+4
source share
1 answer

Given that booleans are mainly used in codes, the second option seems most appropriate.

o = MyClass()
...
if o.is_request: # very intuitive
    # it a request
+1
source

Source: https://habr.com/ru/post/1531828/


All Articles