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:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old April 1st, 2004, 09:42 AM
jimo9 jimo9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 81 jimo9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 41 sec
Reputation Power: 6
Type Checking?

Ok here is what I am trying to do. I have a list and it has some strings in it, but it also has some lists in it. Once I grab an element from the list, how do I check if it is a list or a string, etc. Thanks!

Reply With Quote
  #2  
Old April 1st, 2004, 10:55 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
There are several ways:
Code:
>>> type([1, 2]) is list
True
>>> type([1, 2]) is str
False
>>> isinstance([1, 2], list)
True
>>> isinstance("text", basestring)
True
>>> isinstance("text", str)
True
>>> isinstance(u"text", str)
False
>>> isinstance(u"text", unicode)
True


A string can be of type str or of type unicode. Check if it's an instance of basestring instead.

Last edited by percivall : April 1st, 2004 at 10:57 AM.

Reply With Quote
  #3  
Old April 1st, 2004, 10:56 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,193 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 12 h 59 m 24 sec
Reputation Power: 252
use the type(x) builtin function.

From the manual:

Quote:
type( object)

Return the type of an object. The return value is a type object. The standard module types defines names for all built-in types that don't already have built-in names. For instance:

>>> import types
>>> x = 'abc'
>>> if type(x) is str: print "It's a string"
...
It's a string
>>> def f(): pass
...
>>> if type(f) is types.FunctionType: print "It's a function"
...
It's a function

The isinstance() built-in function is recommended for testing the type of an object.


To check for strings it is safer to use isinstance(x, types.StringTypes), since that will succeed for both regular strings and unicode strings.


Dave - The Developers' Coach

Last edited by DevCoach : April 1st, 2004 at 11:01 AM.

Reply With Quote
  #4  
Old April 1st, 2004, 06:39 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
Quote:
Originally Posted by jimo9
Ok here is what I am trying to do. I have a list and it has some strings in it, but it also has some lists in it. Once I grab an element from the list, how do I check if it is a list or a string, etc. Thanks!

The pythonic way is to not check the type, but simply to do whatever actions you would do to the string and if any object happens to fit those criteria, allow it to do so. That way you can have string-like classes/objects that will also be usable in that function without having to constantly add special cases in your type checking.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #5  
Old April 2nd, 2004, 03:46 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,193 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 12 h 59 m 24 sec
Reputation Power: 252
Quote:
Originally Posted by Strike
The pythonic way is to not check the type, but simply to do whatever actions you would do to the string and if any object happens to fit those criteria, allow it to do so. That way you can have string-like classes/objects that will also be usable in that function without having to constantly add special cases in your type checking.



Good point. If you want to check the type of an object so that you can raise an exception if it not what you expect, then you are better off just using it and seeing what happens.

However there are times when you do want to check the type so that you can take different actions depending on what the object is. A common case is to distinguish between strings and other sequence objects.
Here is an example - a generator that converts a nested sequence into a flattened, non-nested sequence:

Code:
import types

def flatten(seq):
	if isinstance(seq, types.StringTypes):
		yield seq
	elif hasattr(seq, '__iter__'):
		for item in seq:
			for element in flatten(item):
				yield element
	else:
		yield seq
		
>>> x = [['foo, bar', ('one', 'two', 'three')], ('four', 'five'), 4, (6,7)]
>>> list(flatten(x))
['foo, bar', 'one', 'two', 'three', 'four', 'five', 4, 6, 7]
>>> 


If it did not check for a string before checking for a sequence then it would go into an infinite recursive loop, since the characters in a string are also strings.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Type Checking?


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