newbee I have an object how to check what's his class?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbee I have an object how to check what's his class?
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
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.
[EMAIL PROTECTED] 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??
--
http://mail.python.org/mailman/listinfo/python-list
Re: newbee I have an object how to check what's his class?
I think I know now where my problems come from. I spare you boring
implementation code.
The case look like this: I parse xml file something a like
1 <\a>
2 <\a>
3 <\a>
4<\b>
<\X>
<\a>
<\a>
<\a>
<\b>
<\X>
I succesfully constructlop-level X objects - I see all components when i
print X0, X1 out with _repr_.
I store my X's in memory. I had some problems with this. I googled, red
newsgoup ad foud a solution that seemd to be perfect for me (...till now).
In the parser- Y class a have a dictionary
def __init__
self.mem={}
I googled a way how to add elements to dict, I have read the code not the
description below
##copied from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66516
def addItem(self,word,pagenumber):
self.mem.setdefault(word,[]).append(pagenumber)
So my dict looks like{
(id of top-level X) 0 : (the X itself) X0,
2 :X2,
4..
}
I wrote it some time and I was tired I haven't pay attention to the
breackets at he beginning of outprint
TOP LEVEL X WITH ID=0
[ <
1<\a>
2<\a>
3<\a>
4<\a>
<\class X>
<\class X>]
One can clearly see it's a list :( Shame on me.
I did a trick. If my X object is stored in a list and it's the only element
of the list I can simply use it like:
print v[0]
print" isinstance(x,X)"
print isinstance(v[0],X)
print "type(x) is X"
print type(v[0]) is X
print v[0].__class__
print v[0].__class__.__name__
and results
2<\a>
3<\a>
4<\a>
<\class X>
<\class X>
isinstance(x,X)
True:-)
type(x) is X
False:( ??
__main__.X
X
temporarily it solves my probblems I'm just curious why type can't handle
the test.
Thank you for help, and making me think :-)
--
http://mail.python.org/mailman/listinfo/python-list
