SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 10th, 2003, 03:05 PM
Maeriel Maeriel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Canada
Posts: 7 Maeriel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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"

Reply With Quote
  #2  
Old December 10th, 2003, 03:19 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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"

Reply With Quote
  #3  
Old December 10th, 2003, 03:22 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Oh, and btw, str, int, and float are all built-in variables and you shouldn't redefine them.

Reply With Quote
  #4  
Old December 10th, 2003, 03:25 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : December 11th, 2003 at 02:38 AM.

Reply With Quote
  #5  
Old December 10th, 2003, 06:52 PM
Maeriel Maeriel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Canada
Posts: 7 Maeriel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #6  
Old December 10th, 2003, 07:21 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
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"

Reply With Quote
  #7  
Old December 10th, 2003, 11:05 PM
Maeriel Maeriel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Canada
Posts: 7 Maeriel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #8  
Old December 10th, 2003, 11:21 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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.

Reply With Quote
  #9  
Old December 11th, 2003, 01:02 AM
Maeriel Maeriel is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Canada
Posts: 7 Maeriel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
So the except command is what I was looking for. Thanks.

Reply With Quote
  #10  
Old December 12th, 2003, 10:45 PM
TheBlackMamba TheBlackMamba is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 25 TheBlackMamba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to TheBlackMamba Send a message via Yahoo to TheBlackMamba
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)

Reply With Quote
  #11  
Old December 13th, 2003, 01:17 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How do type in the "type" for a comparison operation?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway