Checking an object type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    Checking an object type

    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:
    Code:
    from point import Point
    pt1 = Point(x_coord, y_coord, z_coord)
    print type(pt1)
    <type 'point'>
    print dir(pt1)
    ['dist']
    Original code:
    Code:
            def chk_type(p_list):
                ret_list = []
                for p in p_list:
                    if type(p) ==  type(Point(0,0,0))):
                        ret_list.append(True)
                    else:
                        ret_list.append(None)
                return ret_list
    
            if None not in chk_type([p1, p2, p3]):
                .....do stuff
    Improved code:
    Code:
            def chk_type(p_list):
                for p in p_list:
                    if not isinstance(p, type(Point(0,0,0))):
                        return False
                return True     
            
            if chk_type([p1, p2, p3]):
                ........do stuff
    This does not work:
    Code:
    if not isinstance(p, Point):
    I am confused about what 'type' of object 'pt1' actually is.
  • fuffens
    New Member
    • Oct 2006
    • 38

    #2
    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

    Code:
    class A:
        pass
    
    a1=A()
    aList = [a1, a1, a1]
    for a in aList:
        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

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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:
      Code:
      # use to check type of objects
      from point import Point, PointLocate
      
      p1 = Point(0.0, 0.0, 0.0)
      p2 = Point(1.0, 1.0, 1.0)
      
      print type(p2)
      print type(Point)
      print type(p1.dist)
      
      print dir(p1)
      
      if isinstance(p1, type(Point(0,0,0))):
          print "Success"
      else:
          print "Failure"
      
      if isinstance(p1, object):
          print "SUCCESS"
      else:
          print "FAILURE"
          
      print    
      print "Distance between point p1 and point p2 = %s" % (p1.dist(p2))
      print
      print "Attribute listing for object 'p1':"
      for i in dir(p1):
          try:
              _value_ = eval('p1.' + i)
          except:
              'p1.' + i, "is not a valid attribute or method for this instance."
          else:
              print 'p1.' + i, "=", _value_
              
      import types
      print isinstance(p1, types.ObjectType)
      
      if isinstance(p1, Point):
          print "Success"
      else:
          print "Failure"
      Output from the above script:
      Code:
      <type 'point'>
      <type 'builtin_function_or_method'>
      <type 'builtin_function_or_method'>
      ['dist']
      Success
      SUCCESS
      
      Distance between point p1 and point p2 = 1.73205080757
      
      Attribute listing for object 'p1':
      p1.dist = <built-in method dist of point object at 0x0470CDC0>
      True
      Traceback (most recent call last):
        File "C:/SDS2_7.0/macro/Work In Progress/TypeCheck.py", line 42, in ?
          if isinstance(p1, Point):
      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

      Comment

      • fuffens
        New Member
        • Oct 2006
        • 38

        #4
        This is quite strange. Obviously p1 is an object from the result of isinstance(p1, types.ObjectTyp e) 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

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          bv, check out the inspect module. There a lots of introspection methods there. I hope it exists in 2.3.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by bartonc
            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.
            Code:
            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:
            Code:
            isinstance(pt, type(Point(0,0,0)))

            Comment

            Working...