It is my firm belief that truth testing a value that is not
a Boolean should raise an exception. If you want to convert
a type to Boolean then pass it to the bool function:
lst = [1,2,3]
if bool(lst):
do_something
This would be "explicit enough"
That is *exactly* equivalent to the same test without the bool
function, and it gives the reader zero additional information about
what "lst" is, so it boggles me that you approve of this "if
bool(lst):" monstrosity while decrying the equivalent and slightly
more efficient "if lst:".
I think part of your complaint concerns the fact that the reader must
understand the rules by which a truth value is implicitly obtained
from lst in the statement "if lst:". But the reader must know the
*same* rules in order to understand the more explicit "if bool(lst):",
so there is no benefit to the latter in that regard either.
Yes i do care about the length or i would not have asked.
I'm asking Python to tell me if the iterable has members,
amd if it does, i want to execute a block of code, if it
does not, i want to do nothing. But i'm also informing the
reader of my source code that the symbol i am truth testing
is expected to be an iterable with a __len__ method.
Caring that the object has a length is not the same as caring about
what the object's length is. Steven's point stands, that your code
inefficiently asks the object for some property that you don't (yet)
care about.
"if lst" does not give me the same answer (or imply the same
meaning to a reader), it merely tells me that the implict
conversion has resulted in a True value, but what if the lst
symbol is pointing to a string? Then i will falsely believe
i have a list with members when i actually have a string
with length greater than zero.
Your "if len(lst) > 0" fails to differentiate lists from strings in
exactly the same way.
I agree. Summing the list members just to guarantee that the
iterable has members is foolish, however, python gives me no
other choice IF i want to be "explicit enough". In a
properly designed language, the base iterable object would
supply a "hasLength" or "hasMembers" method that would
return a much faster check of:
try:
iterable[0]
except IndexError:
return False
else:
return True
That check would guarantee the iterable contained at least
one member without counting them all.
You said earlier in your post that "bool(lst)" was "explicit enough",
and this is exactly what it does.
When i am writing code i prefer to be "explicit enough" so
that IF my assumptions about the exact type of an object are
incorrect, the code will fail quickly enough that i can
easily find and correct the problem.
In a duck-typing language you should not be making assumptions about
the "exact type" of an object in the first place. If I'm writing a
function that receives a list-like argument, then at the *most
specific* I will assume that the object passed in is a MutableSequence
(but since I prefer to keep my functions functional where practical,
more usually I will assume only that the object is an iterable or a
generic sequence). If the caller wants to pass in a deque or some
user-defined generic MutableSequence instead, then let them do so. I
will also clearly document that assumption; if the caller can't be
bothered to read the docs and passes in an object that breaks that
assumption, then that's their own damn problem when it doesn't work.
This is a programming language for consenting adults.
But we are really ignoring the elephant in the room. Implict
conversion to Boolean is just a drop in the bucket compared
to the constant "shell game" we are subjected to when
reading source code. We so naively believe that a symbol
named "lst" is a list object or a symbol "age" is a integer,
when we could be totally wrong! This is the source of many
subtle bugs!!!
I am more likely to believe that an object is a list based on the
documentation than on the mere fact that it is named "lst". The
variable *does* have documentation, doesn't it? If when debugging I
have reason to suspect that the documentation is incorrect or is being
ignored, then I'll add an assertion to test it.
There must be some method by which we can truth test an
iterable object and verify it has members, but do so in a
manner that is valid for all types AND exposes the "expected
type" in the method name. hmm...
This is nonsense. If it exposes the "expected type" in the name, then
it can only be valid for that expected type.
Adding a method like "is_valid" to every object can seem
logical, however, this can fail just as miserably as
Python's current implicit bool. And, more disastrously, an
"is_valid" method is not going to raise an error (where it
should) because it works for all types.
Actually it sounds completely illogical to me. What would be an
"invalid" object?
What we need is a method by which we can validate a symbol
and simultaneously do the vaidation in a manner that will
cast light on the type that is expected. In order for this
to work, you would need validators with unique "type names"
if var.is_validList():
elif var.is_validString():
elif var.is_vaildTuple():
elif var.is_validInteger():
elif var.is_validFloat():
elif var.is_validDict():
etc...
How are these any different from the more flexible isinstance? And
where exactly are you going to stop with these is_valid methods?
var.is_validObject()
var.is_validDate()
var.is_validTime()
var.is_validDatetime()
var.is_validSocket()
var.is_validDeque()
var.is_validMutableSequence()
var.is_validSequence()
var.is_validMutableSequencePlusAConcatenateMethod()
var.is_validUserDefinedObjectThatDoesNotExistInTheStandardLibraryAtAll()
And on and on and on. How do you plan to add every single possible
one of these methods to every single object? Remember that if you
miss one, e.g. if you leave is_validString() off of your socket
objects, then you'll get an AttributeError instead of a simple False
value when you try to test it.
By this manner, we can roll three common tests into one
method:
* boolean conversion
* member truthiness for iterables
* type checking
How exactly does this is_valid method perform the first two? Are you
suggesting that an empty sequence should not be considered "valid"?