determine variable type

Discussion in 'Python' started by MCollins@seminolecountyfl.gov, Aug 18, 2005.

  1. Guest

    trying to determine a variable type, specifically that a variable is an
    integer.

    i tried using type(var) but that only seemed to produce a response in the
    command line.

    is there a built in python function to determine if a variable is an
    integer?




    --****Florida has a very broad Public Records Law. Virtually all written
    communications to or from State and Local Officials and employees are
    public records available to the public and media upon request. Seminole
    County policy does not differentiate between personal and business emails.
    E-mail sent on the County system will be considered public and will only
    be withheld from disclosure if deemed confidential pursuant to State
    Law.****
     
    , Aug 18, 2005
    #1
    1. Advertising

  2. Paul Rubin Guest

    writes:
    > i tried using type(var) but that only seemed to produce a response in the
    > command line.
    >
    > is there a built in python function to determine if a variable is an
    > integer?


    type(var) returns the type. For example:

    if type(x) == type(3):
    print 'x is an integer'
     
    Paul Rubin, Aug 18, 2005
    #2
    1. Advertising

  3. BranoZ Guest

    Paul Rubin wrote:
    > writes:
    > > i tried using type(var) but that only seemed to produce a response in the
    > > command line.
    > >
    > > is there a built in python function to determine if a variable is an
    > > integer?

    >
    > type(var) returns the type. For example:
    >
    > if type(x) == type(3):
    > print 'x is an integer'


    or

    x = 5
    isinstance(x, int)
    True

    BranoZ
     
    BranoZ, Aug 18, 2005
    #3
  4. wrote:
    > trying to determine a variable type, specifically that a variable is an
    > integer.
    >
    > i tried using type(var) but that only seemed to produce a response in the
    > command line.
    >
    > is there a built in python function to determine if a variable is an
    > integer?


    if isinstance(var, (int, long)):
    print 'var (%r) is an integer' % var
    elif isinstance(var, float) and int(var) == var:
    print 'var (%r) is a float that could be an integer' % var
    else:
    print 'var (%r) is not an integer' % var

    --Scott David Daniels
    Scott
     
    Scott David Daniels, Aug 18, 2005
    #4
  5. Magnus Lycka Guest

    wrote:
    > trying to determine a variable type, specifically that a variable is an
    > integer.
    >
    > i tried using type(var) but that only seemed to produce a response in the
    > command line.


    You mean that if you do "type(var)" at the Python prompt, it gives
    you a reply, but if you have a line with just that in a script, it
    doesn't?

    That has nothing to do with type checks. All Python expressions in
    Python work like that. In interactive use, unused results are echoed
    to the screen, but in scripts, they are not. How would you expect
    the echoed value to be useful in your program?

    In a script, you need to take care of the output of an expression
    to get any use from it. A standalone expression on a line of its
    own is legal Python code, but the result is thrown away... E.g.

    var = 6
    type(var) # Works, but meaningless, noone will know what happened
    print type(var) # Like the interactive use (more or less)
    var_type = type(var) # Now you have a variable containing the result
    # of the expression. You can use that later
    if type(var) == int: # You can use the result in another construct...
    print "%i is an integer!" % var

    But don't worry so much about type checking. That's typically not
    so important in Python as is is in most other languages, in fact, if
    you try to handle types too strictly in Python, you are throwing away
    a lot of the benefits of Python's dynamic features.

    Perhaps your code is useful with other types than integers, such as
    the new decimal type or some upcoming money type. That typecheck might
    just make it impossible to use your code in some completely meaningful
    way. If you just did nothing, your code would probably throw an
    exception if 'var' had a type that didn't make sense, and handling
    that exception is probably not more difficult than to handle whatever
    action you planned to take if var wasn't an int...

    Python is much stricter in its type handling than e.g. C++, so don't
    worry so much. Trying to add a string to an int etc, will raise a
    sensible exception. It won't lead to any program crash or give any
    bizarre result to your calculations.

    If you're sanitizing user input, that's a good thing of course, but
    then the safe thing would be to read strings (e.g. via raw_input) and
    then inspect the string before you evaluate it or cast it to something
    else. Actually, int() does this pretty well, so you could just do:

    ..while 1: # Loop until broken out of...
    .. response = raw_input('Number please: ')
    .. try:
    .. var = int(response)
    .. break
    .. except ValueError:
    .. print response, 'isn't a number!'
     
    Magnus Lycka, Aug 19, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad

    Determine a session variable type

    tshad, Feb 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,910
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=
    Feb 20, 2006
  2. Gernot Frisch

    how to determine type-type?

    Gernot Frisch, Jan 12, 2005, in forum: C++
    Replies:
    3
    Views:
    400
    Ulrich Achleitner
    Jan 13, 2005
  3. Walter L. Preuninger II

    Way to determine type of variable?

    Walter L. Preuninger II, Jul 16, 2004, in forum: C Programming
    Replies:
    21
    Views:
    566
    Kenny McCormack
    Jul 17, 2004
  4. Replies:
    3
    Views:
    278
    Roedy Green
    Sep 15, 2007
  5. Peña, Botp
    Replies:
    1
    Views:
    242
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page