|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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" |
|
#3
|
|||
|
|||
|
Oh, and btw, str, int, and float are all built-in variables and you shouldn't redefine them.
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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? |
|
#6
|
|||
|
|||
|
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"
|
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
So the except command is what I was looking for. Thanks.
|
|
#10
|
|||
|
|||
|
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) |
|
#11
|
|||
|
|||
|
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).
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How do type in the "type" for a comparison operation? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|