Indentifying types?

O

Oltmans

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
 
M

Mike Driscoll

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.

Mike
 
G

Gabriel Genellina

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.
 
C

Chris Rebert

- Show quoted text -


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
 
M

Mike Driscoll

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

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
 
T

Terry Reedy

Oltmans said:
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

You read the lines of a file as text strings.
You can convert a string with int() or float() or get ValueError if
invalid. Note that decimal int literals are also float literals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,240
Latest member
เพิ่มไลค์|LikePro

Latest Threads

Top