Right way to set a variable to NULL?

John Machin sjmachin at lexicon.net
Fri Dec 26 17:05:47 EST 2008


On Dec 27, 8:16 am, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> Martin wrote:
> > ...
> > class MailAddress(object):
> >   def __init__(self, address=None):
> >     self.address = address
> >   def __str__(self):
> >     if address:
> >       return self.adress
> >     return "NULL"
>
> There is an obvious typo above:
>  >     if address:
> should be:
>        if self.address:
>
> Or, you could replace the __str__ function with:
>      def __str__(self):
>          return self.address or "NULL"

The above all have the same characteristic: if the input is a zero-
length string, then NULL is inserted into the database instead of a
zero-length string. Some folks (not just pedants!) regard that as an
important difference.




More information about the Python-list mailing list