473,378 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Checking an object type

bvdet
2,851 Expert Mod 2GB
Python 2.3 Windows XP
We have a builtin module 'point'. I am trying to determine a better way to check the type of a list of 'point' objects. A point object is created:
Expand|Select|Wrap|Line Numbers
  1. from point import Point
  2. pt1 = Point(x_coord, y_coord, z_coord)
  3. print type(pt1)
  4. <type 'point'>
  5. print dir(pt1)
  6. ['dist']
Original code:
Expand|Select|Wrap|Line Numbers
  1.         def chk_type(p_list):
  2.             ret_list = []
  3.             for p in p_list:
  4.                 if type(p) ==  type(Point(0,0,0))):
  5.                     ret_list.append(True)
  6.                 else:
  7.                     ret_list.append(None)
  8.             return ret_list
  9.  
  10.         if None not in chk_type([p1, p2, p3]):
  11.             .....do stuff
Improved code:
Expand|Select|Wrap|Line Numbers
  1.         def chk_type(p_list):
  2.             for p in p_list:
  3.                 if not isinstance(p, type(Point(0,0,0))):
  4.                     return False
  5.             return True     
  6.  
  7.         if chk_type([p1, p2, p3]):
  8.             ........do stuff
This does not work:
Expand|Select|Wrap|Line Numbers
  1. if not isinstance(p, Point):
  2.  
I am confused about what 'type' of object 'pt1' actually is.
Dec 6 '06 #1
5 24238
fuffens
38
Hi bvdet!

Can you specify what's not working with the isinstance expression? I do not have the point module so I created this simple script

Expand|Select|Wrap|Line Numbers
  1. class A:
  2.     pass
  3.  
  4. a1=A()
  5. aList = [a1, a1, a1]
  6. for a in aList:
  7.     print isinstance(a, A)
Here all a in the list are instances of A. I am using version 2.2.3 of Python.
Best regards
/Fredrik
Dec 6 '06 #2
bvdet
2,851 Expert Mod 2GB
Fredrik,

I do not have access to the source code for the point module, so I cannot determine what is going on. Since a point object has a method (pt.dist()), I thought it must be an instance. It's not though. Here's part of a script that I use to check types:
Expand|Select|Wrap|Line Numbers
  1. # use to check type of objects
  2. from point import Point, PointLocate
  3.  
  4. p1 = Point(0.0, 0.0, 0.0)
  5. p2 = Point(1.0, 1.0, 1.0)
  6.  
  7. print type(p2)
  8. print type(Point)
  9. print type(p1.dist)
  10.  
  11. print dir(p1)
  12.  
  13. if isinstance(p1, type(Point(0,0,0))):
  14.     print "Success"
  15. else:
  16.     print "Failure"
  17.  
  18. if isinstance(p1, object):
  19.     print "SUCCESS"
  20. else:
  21.     print "FAILURE"
  22.  
  23. print    
  24. print "Distance between point p1 and point p2 = %s" % (p1.dist(p2))
  25. print
  26. print "Attribute listing for object 'p1':"
  27. for i in dir(p1):
  28.     try:
  29.         _value_ = eval('p1.' + i)
  30.     except:
  31.         'p1.' + i, "is not a valid attribute or method for this instance."
  32.     else:
  33.         print 'p1.' + i, "=", _value_
  34.  
  35. import types
  36. print isinstance(p1, types.ObjectType)
  37.  
  38. if isinstance(p1, Point):
  39.     print "Success"
  40. else:
  41.     print "Failure"
Output from the above script:
Expand|Select|Wrap|Line Numbers
  1. <type 'point'>
  2. <type 'builtin_function_or_method'>
  3. <type 'builtin_function_or_method'>
  4. ['dist']
  5. Success
  6. SUCCESS
  7.  
  8. Distance between point p1 and point p2 = 1.73205080757
  9.  
  10. Attribute listing for object 'p1':
  11. p1.dist = <built-in method dist of point object at 0x0470CDC0>
  12. True
  13. Traceback (most recent call last):
  14.   File "C:/SDS2_7.0/macro/Work In Progress/TypeCheck.py", line 42, in ?
  15.     if isinstance(p1, Point):
  16. TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
The only builtin type that returns true is 'ObjectType'. My code works, but I dislike not knowing what is going on. Thanks for the post Fredrick.

Have a good one!
BV
Dec 7 '06 #3
fuffens
38
This is quite strange. Obviously p1 is an object from the result of isinstance(p1, types.ObjectType) and one would think that Point is a class. But it could be that Point() is an object factory function that returns an object. Unfortunately, type does only say instance and not the type of class.

BR
/Fredrik
Dec 7 '06 #4
bartonc
6,596 Expert 4TB
bv, check out the inspect module. There a lots of introspection methods there. I hope it exists in 2.3.
Dec 7 '06 #5
bvdet
2,851 Expert Mod 2GB
bv, check out the inspect module. There a lots of introspection methods there. I hope it exists in 2.3.
That's a good thought Barton, but it does not help much. Module 'point' is compiled and embedded in sds2.exe, the main executable file for SDS/2 www.sds2.com.
Expand|Select|Wrap|Line Numbers
  1. inspect.getmodulename(Point)
The above call fails, and an odd directory path is listed in the traceback. I found out that module 'point' was written in C and is probably 6 years old. There are several other modules as well. THANKS for your help guys. I will settle on this type check code:
Expand|Select|Wrap|Line Numbers
  1. isinstance(pt, type(Point(0,0,0)))
Dec 7 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James Marshall | last post by:
I need to detect the type of an object, more than just "object" as typeof gives us. I'm writing a general handler that accepts a variety of objects and properties, and acts accordingly depending...
9
by: Arash Dejkam | last post by:
Hi All, Is it possible to write on an <OBJECT type="text/html"> using document.write() from within the html containing that tag the way we write on a popup window? I couldn't do that after a lot...
0
by: Dan Noel | last post by:
I am implementing a DataStore/Settings object that stores objects in XML by serializing the objects in XML and then associating these objects with key names. At some later point, I want to...
1
by: Ping | last post by:
Hi, i am trying to : dim db As database and i have Access 2002. The object type "database" is not in the list and error message is "type mismatching " when run. i am wondering if the...
1
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which...
2
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType();...
3
by: cv | last post by:
How to cast a System.Object type to a class instance? For example, Form1 has a Listbox. I create a custom class MyClass and add several instances of that class to the Listbox. When the user...
6
by: Picho | last post by:
Hi all. I have a webservice and a windows app. both of them reference the same class library called WebServiceTest.Core that defines a class called Class1. the webservice exposes a method...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
1
by: urkel | last post by:
Hi everyone, I critically need help to solve this problem related to pointer in C++ Basically, I have a C/C++ program "retardselfenerg" calling a Fortran 90 subroutine "surfGF4.f90". i am so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.