Type constants?

Fredrik Lundh fredrik at pythonware.com
Tue May 1 11:05:31 EDT 2001


Roy Smith wrote:
> Is there a way of writing type('') as a constant?  What I want to do
> is check to see if an object is a string.  The best I can think of is
> to do something like:
>
> if type(foo) == type('')
>
> which would work, but seems kind of silly.  Is there no pre-defined
> object somewhere whose value is <type 'string'>?

    import types

    if type(foo) is types.StringType:
        ...

(which is nearly as silly, imo)

for extra style points, use:

    if isinstance(foo, types.StringType):
        ...

Cheers /F





More information about the Python-list mailing list