Indentifying types?

Mike Driscoll kyosohma at gmail.com
Tue Mar 3 19:20:29 CET 2009


On Mar 3, 11:44 am, Chris Rebert <c... at rebertia.com> wrote:
> On Tue, Mar 3, 2009 at 9:03 AM, Mike Driscoll <kyoso... at gmail.com> wrote:
> > - Show quoted text -
> > On Mar 3, 10:57 am, Oltmans <rolf.oltm... at gmail.com> wrote:
> >> 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.
>
> >> Thanks,
> >> Oltmans
>
> > I think when you're reading from a file, it will just read each line
> > as a string. So you'd probably need to either try casting the line
> > into something else and catch it in an exception handler or use eval.
>
> > The normal way to check types is to use the keyword isinstance or just
> > use the "type" keyword.
>
> isinstance() and type() are callables, *not* keywords; and IMHO,
> type() should never be used for typechecking (ironically), since such
> checks are always written more clearly using isinstance().
>
> Cheers,
> Chris
>
> --
> I have a blog:http://blog.rebertia.com

Yeah, I never use type except in IDLE or for debugging purposes. But
IDLE does change their color, so they are builtins, so I thought
keywords was a more understandable term for a newb.

Mike



More information about the Python-list mailing list