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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabian Steiner

    #1

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

    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
  • Scott David Daniels

    #2
    Re: General Type Checks (int, str, tuple, etc.)

    Fabian Steiner wrote:[color=blue]
    > 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?[/color]

    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
    scott.daniels@a cm.org

    Comment

    Working...