newbee I have an object how to check what's his class?

consternation copiernik at wp.pl
Fri Nov 10 10:01:02 EST 2006


I can't find  neither in tutorial nor with google It's all about 
isinstance,  or  __class__.
How to test that an object is an instance of my X class??
Do I have this problems because I stre my objects in a dict?

I wrote a class X like this :
class X(object):

     def __init__(self,name):
       self.name=name
       self.val=[]
       self.description ="class X contains : "

def __repr__(self):
         for i in range(len(self.val)):
            description+=i
             return self.description


In class Y I create my X objects and put them into a dict

  print "\nTEST"
.for (i,v) in self.mem.items():
      print v

The objects are printed out the way I specified in  __repr__, so I know it's 
an object of X class.
No I want to put in the dict some other objects of class Z,K....
When I get the value fom dict I have to distinguish them somehow to handle 
them latr in programm.
I thouth about isinstanceof  - it doesn't work. I did some tests, but I 
don't understand the answers:
Why python claims it's a list, but still print's it like X class
#in Y class:
print isinstance(v,X)                      False
print v.__class__.__name__         list


And adding print in  X class i see
 def __repr__(self):
    print self.__class__            -->        [__main__.Complex

Could someone explain this to me?
thank you





More information about the Python-list mailing list