Sorry, keep forgetting "reply-all"<br><br>---------- Forwarded message ----------<br><span class="gmail_quote">From: <b class="gmail_sendername">Kenny Li</b> <<a href="mailto:kenny.li@gmail.com">kenny.li@gmail.com
</a>><br>Date: Feb 13, 2006 11:00 AM<br>Subject: Re: [Tutor] Instantiate a subClass using an instance of its baseClass<br>To: Kent Johnson <<a href="mailto:kent37@tds.net">kent37@tds.net</a>><br><br></span>
<div>Yep! I think that is what I want. I did not know enough to do (inside C.__init__() ):</div>
<div> arg1.__class__=C</div>
<div> </div>
<div>As to my intent, I want to subclass socket.socket. By doing so, I may get a socket (instance of my baseClass) after it (my subclass) accepts an incoming connection, and I want to use the subclass instance (the client socket) to get an instance of my subclass.
</div>
<div> </div>
<div>Do you think your solution would work in my socket.socket subclassing case? (I will give it a try to verify, just asking before I even try)</div>
<div> </div>
<div>Again, Thanks a lot, Kent!</div>
<div> </div>
<div>PS: there is information about __new__(), but there seems not a lot about __class__, could you point me to some web sources about __class__? </div>
<div><br> </div>
<div><span class="q"><span class="gmail_quote">On 2/13/06, <b class="gmail_sendername">Kent Johnson</b> <<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:kent37@tds.net" target="_blank">kent37@tds.net
</a>> wrote:</span> </span>
<div><span class="e" id="q_10964c9693879d1e_2">
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Kenny Li wrote:<br>> Kent:<br>><br>> I forgot to mention that no coping is allowed. Your two options
<br>> essentially are doing copying of the b.<br><br>Not really. I make new references to the attributes of b.<br><br>It sounds like you want<br> c = C(b)<br>to actually convert b to be an instance of C, instead of creating a new
<br>object. Is that right? You can do that it C.__new__ but I wonder why you<br>want to?<br><br>Here is some code that might do what you want:<br><br>''' Construction of a derived class from a base class returns the base class
<br> converted to an instance of the derived class '''<br><br>class B(object):<br> ''' the baseClass '''<br> def __init__(self, arg1, arg2):<br> self.a1=arg1<br> self.a2=arg2<br><br> def __repr__(self):
<br> return 'B(%r, %r)' % (self.a1, self.a2)<br><br>class C(B):<br> ''' C is subClass of B '''<br> def __new__(cls, arg1, arg2=None):<br> if isinstance(arg1, B):<br> # If given a B instance, convert it to a C and return it
<br> arg1.__class__ = C<br> return arg1<br><br> return B.__new__(cls, arg1, arg2)<br><br> def __init__(self, arg1, arg2=None):<br> if not isinstance(arg1, B):<br> B.__init__(self, arg1, arg2)
<br> self.extra="blah blah blah"<br><br> def __repr__(self):<br> return 'C(%r, %r, %r)' % (self.a1, self.a2, self.extra)<br><br><br>b=B(1, 2)<br>print b<br><br>c=C(2, 3)<br>print c<br><br>c=C(b)
<br>print c, (c is b)<br>print b # b is now a C - it's the same object as c<br><br><br>Kent<br><br>_______________________________________________<br>Tutor maillist - <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:Tutor@python.org" target="_blank">
Tutor@python.org</a> <br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></span>
</div></div><br>