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

Gregor Lingl glingl at aon.at
Tue Aug 12 22:20:10 EDT 2003


A.M. Kuchling schrieb:

>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)'.
>  
>
...

>'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.
>  
>
Until now I couldn't find out if there are names for some of the 
built-in types.

In this case:
 >>> b=u""
 >>> type(b)==unicode
True
 >>> type(b)==base_string

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in -toplevel-
    type(b)==base_string
NameError: name 'base_string' is not defined

So the type object refering to the above mentioned
built-in type base_string has another identifier?

Equally I'd like to know if there are type-objects for
the built-in types "function" and "generator".

What's the "canonical way" to find out if an object is
a function or a generator?

Regards, Gregor






More information about the Tutor mailing list