#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Canada
    Posts
    7
    Rep Power
    0

    How do type in the "type" for a comparison operation?


    Lets say I want to compare the type of some varible. How do I type in the actual type in it with out doing something like this:

    ex.
    Code:
    x = "blah"
    str = "string"
    int = 100
    float = 10.1
    
    if type(x) == type(str)
    _____print "String"
    elif type(x) == type(int)
    _____print "Int"
    elif type(x) == type(float)
    _____print "float"
    else:
    _____print "unknown"
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    You should use isinstance instead:

    Code:
    if isinstance(x, str):
       print "str"
    elif isinstance(x, int):
       print "int"
    elif isinstance(x, float):
       print "float"
    else:
       print "unknown"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Oh, and btw, str, int, and float are all built-in variables and you shouldn't redefine them.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Quick and easy. Use the __builtin__ isinstance() function . this works with your own classes too... type isn't used much anymore, at least in my experiance!

    >>> isinstance(x, str)
    True
    >>> isinstance(x, int)
    False
    >>> isinstance(x, float)
    False
    >>> isinstance(x, object)
    True
    >>>

    And as an if-else statment...

    Code:
    if isinstance(x, str):
        ...
        do whatever
        ...
    elif isinstance(x, int):
        ...
        do something else
        ...
    else:
        ...
        do this
        ...
    Edit: looks like you beet me too it Strike , anyway while i think about it heres another less elegent way to do it (But i don't sugest you do it like this!)

    >>> x = 'string'
    >>> xtype = str(type(x))
    >>> 'str' in xtype
    True
    >>> ' int ' in xtype
    False
    >>> x = 1
    >>> xtype = str(type(x))
    >>> 'int' in xtype
    True
    >>> type(x) == int
    True

    Have fun,
    Mark.
    Last edited by netytan; December 11th, 2003 at 02:38 AM.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Canada
    Posts
    7
    Rep Power
    0
    The reason why I'm asking this is that I'm trying to make a process that accepts all three types of inputs, or is able to detect the type of input and tell the user only to use one input.

    Ex:
    Computer Displays: What year is it?
    Grumpy User puts: 2003 dumbass computer.

    If your using the input() function, it craps out.
    If your using the the raw_input() function, you cannot convert it to a malleble number.

    So is there way to catch something when it outputs an error and tell the user to only put numbers or do I have to use this 50 to 100 line function I made to do this?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    I don't really understand what you're having a problem with, maybe I'm missing something but can't you just do something like:
    Code:
    a = raw_input("choose a number: ")
    
    if type(a) != 'int':
    	print "you must input a number"
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Canada
    Posts
    7
    Rep Power
    0
    the raw_input() function creates a string _no_matter_what_ you type in. (and how you compared it wouldn't work , since the type() function doesn't give a string as output but some sort of wierd type called "type").

    If you use input(), it will only accept numbers so if the user puts some sort of string type thing like a charecter into it, python would spit an error out and stop the entire program.

    I don't really want to use some 100 line function so I could differientiate between the three user inputs, I would rather use some built in C module function and take less processing power.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Use raw_input() and then try and coerce it to each type in a try/except block:
    Code:
    foo = raw_input("choose a number: ")
    
    try:
        foo = int(foo)
    except ValueError:
        print "You must input a number"
    You can sequence and/or nest these try/except blocks to try for more types.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Canada
    Posts
    7
    Rep Power
    0
    So the except command is what I was looking for. Thanks.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    25
    Rep Power
    0
    Actually, you'd pro*a*ly want:

    Code:
    while 1:
    	try:
    		q = input("Insert Prompt Here:")
    		break
    	except SyntaxError: pass
    	except NameError: pass
    Sorry for the *s, my key*oard is *roken.
    * = chr(98)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    No, you pretty much never want to use input() instead of raw_input(). The way I mentioned is the way I've seen from most python programmers use (ie, it's the "pythonic" way of doing it).

IMN logo majestic logo threadwatch logo seochat tools logo