Testing if an object's type is builtin

Reply

Join Date: Jul 2008
Posts: 905
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 214
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Testing if an object's type is builtin

 
0
  #1
Oct 21st, 2009
Does anyone know how I can test if an object's type is a builtin type or a user defined type ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster
 
1
  #2
Oct 21st, 2009
Originally Posted by Gribouillis View Post
Does anyone know how I can test if an object's type is a builtin type or a user defined type ?
If it's not builtin, it must be imported from another module.
  1. >>> getattr(list, '__module__')
  2. '__builtin__'
  3. >>>
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 905
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 214
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #3
Oct 22nd, 2009
Originally Posted by solsteel View Post
  1. >>> getattr(list, '__module__')
  2. '__builtin__'
  3. >>>
Yes, it's a good idea, for the most basic types. In fact the question is ambiguous. Types defined in C extension modules are not detected with this method. For example
  1. >>> from collections import deque
  2. >>> print(deque.__module__)
  3. collections
but in fact the deque type lives in a C library _collections.so . I don't know how to detect this automatically.
Reply With Quote Quick reply to this message  
Reply

Tags
builtin, type

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC