compare types?

Gerhard Häring gerhard.haering at gmx.de
Tue Dec 31 23:53:11 EST 2002


* Dan Bishop <danb_83 at yahoo.com> [2002-12-31 13:30 -0800]:
> Kenneth Godee <Ken at perfect-image.com> wrote in message news:<mailman.1041318305.18298.python-list at python.org>...
> > I'm trying to iterate thru a tuple and find 
> > any DateTime objects.....
> > 
> > print type(item)
> > returns....
> > <type 'DateTime'>
> > 
> > So I thought I could....
> > 
> > if type(item) == 'DateTime': (or many other variations I tried)
> > just can't seem to get a match
> 
> Of course you can't.  type doesn't return a string.
> 
> if type(item) == DateTime:
> would work if you're looking for exact type equality, but you probably want

<nitpick>
It's best to compare to singletons (like types) using the 'is' operator,
because you're really interested in identity here, not equality.

The type of a DateTime object is also DateTimeType.
</nitpick>

> if isinstance(item, DateTime):
> which would also be true if type(item) were a subclass of DateTime.

This cannot be said often enough ;-)

Gerhard
-- 
Favourite database:             http://www.postgresql.org/
Favourite programming language: http://www.python.org/
Combine the two:                http://pypgsql.sf.net/
Embedded database for Python:   http://pysqlite.sf.net/





More information about the Python-list mailing list