Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Lots of Python objects are falsey: empty lists, empty strings, etc. So it's never a good idea to write "if <thing>" when you mean "if <thing> is not None".

This is pretty well-known, I thought.



A big part of it is about user expectations. I would be shocked if any experienced Python programmer who wasn't familiar with this exact implementation detail expected the following:

    if datetime.time(0, 0, 0):
        print "foo"
    else:
        print "bar"
to print "bar"


Oh man people that have used python for a while have come to expect datetime to just be strange in general. Essentially the default is to be in your TZ from midnight, you need to convert and there are different classes for epoch, and some of the members seemingly willy-nilly are not there or named differently then! And that's just the start, that's why things like arrow exist: http://crsmithdev.com/arrow/ time and datetime in python show their age.


Falsey objects mean "there's no data here" which is why they're falsey. Having an empty list of arguments to a command is the same as having no argument list.

There's nothing about midnight that makes sense for it to behave as Falase.


But, it's represented internally as zero. That's reason enough!


That's an implementation detail and should be open to change without breaking things.


My post was meant to be sarcastic. I thought it was ridiculous enough to not need something indicating sarcasm though...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: