[Tutor] Fwd: Instantiate a subClass using an instance of its baseClass

Kenny Li kenny.li at gmail.com
Mon Feb 13 20:02:17 CET 2006


Sorry, keep forgetting "reply-all"

---------- Forwarded message ----------
From: Kenny Li <kenny.li at gmail.com>
Date: Feb 13, 2006 11:00 AM
Subject: Re: [Tutor] Instantiate a subClass using an instance of its
baseClass
To: Kent Johnson <kent37 at tds.net>

Yep! I think that is what I want. I did not know enough to do (inside
C.__init__() ):
        arg1.__class__=C

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.

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)

Again, Thanks a lot, Kent!

PS: there is information about __new__(), but there seems not a lot about
__class__, could you point me to some web sources about __class__?


On 2/13/06, Kent Johnson <kent37 at tds.net> wrote:
>
> Kenny Li wrote:
> > Kent:
> >
> > I forgot to mention that no coping is allowed. Your two options
> > essentially are doing copying of the b.
>
> Not really. I make new references to the attributes of b.
>
> It sounds like you want
>   c = C(b)
> to actually convert b to be an instance of C, instead of creating a new
> object. Is that right? You can do that it C.__new__ but I wonder why you
> want to?
>
> Here is some code that might do what you want:
>
> ''' Construction of a derived class from a base class returns the base
> class
>     converted to an instance of the derived class '''
>
> class B(object):
>     ''' the baseClass '''
>     def __init__(self, arg1, arg2):
>         self.a1=arg1
>         self.a2=arg2
>
>     def __repr__(self):
>         return 'B(%r, %r)' % (self.a1, self.a2)
>
> class C(B):
>     ''' C is subClass of B '''
>     def __new__(cls, arg1, arg2=None):
>         if isinstance(arg1, B):
>             # If given a B instance, convert it to a C and return it
>             arg1.__class__ = C
>             return arg1
>
>         return B.__new__(cls, arg1, arg2)
>
>     def __init__(self, arg1, arg2=None):
>         if not isinstance(arg1, B):
>             B.__init__(self, arg1, arg2)
>         self.extra="blah blah blah"
>
>     def __repr__(self):
>         return 'C(%r, %r, %r)' % (self.a1, self.a2, self.extra)
>
>
> b=B(1, 2)
> print b
>
> c=C(2, 3)
> print c
>
> c=C(b)
> print c, (c is b)
> print b # b is now a C - it's the same object as c
>
>
> Kent
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060213/ac4816f1/attachment.html 


More information about the Tutor mailing list