Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practices to return Boolean value from function- Hardcode or from variable

Tags:

python

boolean

Which is best practices to return Boolean value from a function:

  1. Hardcoded
  2. as a variable

Code 1 :

    (status, response) = self.generate_pdf(html_data)
    if not status:
        return (False, response)

Code 2:

    (status, response) = self.generate_pdf(html_data)
    if not status:
        return (status, response)

According to me returning hardcode value (False or True) is good.

[More details] I always return two parameter from every function, status and response. A status is Boolean object i.e. value is True or False. A response is dictionary object.

As status have a value False i.e. status is referring address of False.

>>> id(False)
493227104
>>> status = False
>>> id(status')
493227104 

So returning reference variable name is good practices or actual bool value ?

like image 860
Vivek Sable Avatar asked Mar 05 '23 04:03

Vivek Sable


1 Answers

This question is quite opinion based, but I would point out a difference between the two approaches: the if not status condition will happen if status is any false-y - an actual False, a 0, None, etc.

The first approach will always return False. The second will retain whatever original false-y caused the condition to evaluate. Whether the first or the second is more appropriate depends on your program and some more context that wasn't provided in the question.

like image 136
Mureinik Avatar answered Apr 26 '23 17:04

Mureinik