Domanda

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?

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top