Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: naming of boolean/flag class attributes [closed]

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?

like image 701
Bernd Avatar asked Mar 14 '14 17:03

Bernd


1 Answers

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
like image 125
Jayanth Koushik Avatar answered Oct 25 '22 07:10

Jayanth Koushik