Python : is it ok returning both boolean and string?
--
Track title: CC P Beethoven - Piano Sonata No 2 in A
--
Chapters
00:00 Question
01:47 Accepted answer (Score 8)
02:29 Answer 2 (Score 27)
02:42 Answer 3 (Score 11)
03:18 Answer 4 (Score 6)
04:07 Thank you
--
Full question
https://stackoverflow.com/questions/6578...
Answer 2 links:
[manual]: http://docs.python.org/reference/datamod...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ANSWER 1
Score 28
Would it not be more suitable to return a None instead of False?
ANSWER 2
Score 12
I believe the orthodox Python design would be to return None. The manual says:
None
This type has a single value. There is a single object with this value. This object is accessed through the built-in name None. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false.
ANSWER 3
Score 6
The convenient thing is to return an empty string in this case.
Besides an empty string in Python will evaluate to False anyway. So you could call it like:
if Myfunc(s, timeout):
    print "success"
Addition: As pointed out by S.Lott the true Pythonic way is to return None. Though I choose to return strings in string related funcs. A matter of preference indeed.
Also I assume the caller of Myfunc only cares about getting a string to manipulate on - empty or not. If the caller needs to check about timeout issues, etc.. it's better to use exceptions or returning None.
ANSWER 4
Score 4
Maybe if you return a tuple like (False, None) and (True, test) it would be better, as you can evaluate them separatedly and not add unnecesary complexity.
EDIT: Perhaps the string that appeared on the serial port is "" (maybe expected), so returning True can say that it arrived that way.