General Type Checks (int, str, tuple, etc.)

F

Fabian Steiner

Hello!

So far, I am using something like »if isinstance(var, int):« to
determine, whether var's value is an integer. Now I would like to know
if there is any better possibility to do such general checks or may a
construct with isinstance() even fail in certain cases?

Cheers,
Fabian
 
S

Scott David Daniels

Fabian said:
Hello!

So far, I am using something like »if isinstance(var, int):« to
determine, whether var's value is an integer. Now I would like to know
if there is any better possibility to do such general checks or may a
construct with isinstance() even fail in certain cases?

The general rule is: don't check, let it fail if it wants to.
If you are looking for an integral value check, you might prefer:

isinstance(x, (int, long))

but again, think long and hard about your requirements. Embrace
dynamic typing; don't try to write your old language in Python.

--Scott David Daniels
(e-mail address removed)
 

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

Members online

No members online now.

Forum statistics

Threads
474,282
Messages
2,571,404
Members
48,095
Latest member
WalkerBore

Latest Threads

Top