Indentifying types?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Mar 3 12:36:53 EST 2009


En Tue, 03 Mar 2009 14:57:29 -0200, Oltmans <rolf.oltmans at gmail.com>  
escribió:

> I'm reading from a file that contains text like
>
> ----
> 5
> google_company
> apple_fruit
> pencil_object
> 4
> test_one
> tst_two
> ----
>
> When I read the integer 5 I want to make sure it's an integer.
> Likewise, for strings, I want to make sure if something is indeed a
> string. So how do I check types in Python? I want to check following
> types
>
> 1- integers
> 2- strings
> 3- testing types of a particular class
> 4- decimal/floats
>
> Please excuse my ignorance & enlighten me. I will really appreciate
> any help.

Item 3 requires special knowledge of the class.
You cannot be sure of the "original" data type. 5 looks like an integer,  
but it's a string too. You may arrange your tests for "most strict" to  
"less strict":

int
float
string

and for each of them, try to do the conversion. If you don't get an  
exception, fine; else continue with the next one.

-- 
Gabriel Genellina




More information about the Python-list mailing list