0

This question is out of itch of being perfect.

I come across the case where I have to get parameter from post and check if it is True or False (in python), and accordingly call the LOC.

Obviously parameter read is of type <str> and if param: always return True.

I had two options now,
1. Convert <str> to <bool> (write own code to convert, or use ast.literal_eval or import from distutils.util import strtobool )
2. Do the string comparison like if param == "True":

The question is what would be the best practice to follow?

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61

2 Answers2

2

I would certainly not go down the route of converting the string to a boolean, that's too much overhead for a simple logic statement. You should first ensure the parameter is either of the values 'True' or 'False'.

Then:

if (param == 'True'):
    # True code here
else:
    # False code here
  • You are right, the read `` parameter has already taken the memory, and converting it to bool will add additional overhead. – Kishor Pawar Jun 03 '16 at 13:36
0

Memory Considerations:

For String
sys.getsizeof("True")
>> 41
sys.getsizeof("False")
>> 42

For Boolean
sys.getsizeof(True)
>> 24
sys.getsizeof(False)
>> 24
Akash Lodha
  • 100
  • 9