[Tutor] Testing a variable for being a number, string or other object...

A.M. Kuchling amk at asti-usa.com
Tue Aug 12 14:25:34 EDT 2003


On Tue, Aug 12, 2003 at 10:10:49AM -0400, Marc Barry wrote:
> from types import IntType
> from types import StringType

The 'types' module isn't being deprecated, but its use is being
discouraged.  Starting in Python 2.2, built-ins such as 'int' and
'float' are really type objects, so you can write 'isinstance(an_int, int)'.

In most cases the isinstance() function can be used instead of calling type()
and comparing type objects, meaning that instead of writing:

	if type(param) == type(''):
	    ...

you can write the clearer code:

	if isinstance(param, str):
            ...

'str' and 'unicode' are two different types; if you want to check for 
either of them, you can use the base_string built-in type.

--amk                                            (www.amk.ca)
My nose shall never be touched while heaven give me strength.
      -- Sterne, _Tristram Shandy_





More information about the Tutor mailing list