Re: Boolean tests [was Re: Attack a sacred Python Cow]



Steven D'Aprano wrote:
On Wed, 30 Jul 2008 09:23:05 -0600, Matthew Fitzgibbons wrote:

If you're expecting a list (and only a list)
then your point makes sense. 'if x' can get you into trouble if you
_don't_ want its polymorphism.

"if x" is hardly unique in that way. If you're expecting a list, and only a list, "len(x) != 0" will get you in trouble if somebody passes a string or a dictionary. I don't see any reason why we should single out "if x" as dangerous in the face of invalid types. With the exception of the "is" and "is not" operators, nothing in Python is guaranteed to work with any imaginable object. Even print can fail, if the object's __str__ method raises an exception.

Forgot respond to this point. I heartily agree. :) I singled out 'if x' only because that's the specific example under scrutiny. I think you and I are pretty much in agreement.

-Matt
.



Relevant Pages

  • Re: Boolean tests [was Re: Attack a sacred Python Cow]
    ... "len!= 0" will get you in trouble if somebody passes a string or a dictionary. ... With the exception of the "is" and "is not" operators, nothing in Python is guaranteed to work with any imaginable object. ... Yes, I know it breaks duck typing, which is why I do it only very rarely, and never with e.g. sequence types. ... If I use ifinstance for type checking, it's because I need some _very_ specific class, almost always one that I wrote. ...
    (comp.lang.python)
  • Re: Boolean tests [was Re: Attack a sacred Python Cow]
    ... "len!= 0" will get you in trouble if somebody passes a string or a dictionary. ... With the exception of the "is" and "is not" operators, nothing in Python is guaranteed to work with any imaginable object. ... Yes, I know it breaks duck typing, which is why I do it only very rarely, and never with e.g. sequence types. ... If I use ifinstance for type checking, it's because I need some _very_ specific class, almost always one that I wrote. ...
    (comp.lang.python)
  • Error when attaching multiple files (System.web.mail)
    ... I am having some trouble using the MailMessage object to send an email with ... Exception has been thrown by the target of an invocation. ... When a user submits a file, it is stored directly to the database, and when ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: SORN off the road?
    ... I think it's only in exception circumstances that you'll get into ... trouble. ... For example I had a kit car which I failed to update the address on the ... just write a letter explaining and I should be OK. ...
    (uk.rec.cars.maintenance)
  • Re: Accessing file while it is being copied
    ... The trouble is that if the app detects a new file before it has been fully copied from another location, and it starts processing, then I get errors. ... I tried to test for an exclusive file lock using NIO, ... If it throws this exception just wait for a few seconds and try again. ...
    (comp.lang.java.programmer)