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

consternation copiernik at wp.pl
Fri Nov 10 11:58:14 EST 2006


Thank You for reply but ....
I found solution suggested by You in a tutorial yesterday. For some reason
it doesn't work in my case.

code:
#mem-dictionary in Y class for storing objects
#Y doesn't inherit from X
      for (i,v) in self.mem.items(): 
      print " isinstance(x,X)"
      print  isinstance(v,X)
      print "type(x) is X"
      print type(v) is X
      print v.__class__.__name__
      print v.__class__

result:
isinstance(x,X)
False
type(x) is X
False
<type 'list'>
list

Well I can handle my problem. I will give an extra field in class with it's
name. But I thought that when a language has tools to learn a class of an
object one should use it. 


paul.keating at nibc.com wrote:

> This doesn't answer your whole post because it asked a lot of
> questions. But as to finding out whether something is an instance of a
> class:
> 
> class X(object):
>       # ... defined as in your post
> 
>>>> x = X('Fred')
>>>> x
> class X contains:
>>>> type(x) is X
> True
>>>> isinstance(x,X)
> True
>>>> x.__class__.__name__
> 'X'
> 
> Now for subclasses:
> 
> class Y(X):
>     extrastuffinY = 1
> 
>>>> y = Y('Joe')
>>>> type(y) is X
> False
>>>> isinstance(y,X)
> True
> 
> 
> consternation:
> 
>> 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??




More information about the Python-list mailing list