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

Kenny Li kenny.li at gmail.com
Mon Feb 13 08:38:10 CET 2006


*Hi Tutor:*
**
*My question is "how to instantiate a subClass using an instance of its
baseClass?"*

*Details below:*
*=============*
**
*class B(object):  *
*    ''' the baseClass '''*
*    def __init__(self, arg1, arg2):*
*        self.a1=arg1*
*        self.a2=arg2*
* *
*class C(B): *
*        ''' C is subClass of B '''*
*        def __init__(self, arg1, arg2):*
*            B.__init__(self, arg1, arg2)*
*            self.extra="blah blah blah"*
**
*if __name__ == '__main__':*
*    c=C("spam", "ham")   #<<<< Normal instantiation, No Problem Here.*
*    *
*    #  Now, I ran into a situation, where I don't have the values of "arg1
and arg2", *
*    #  but I do have an instance of baseClass (B), called b. *
**
*    #  How do I write the class C [for example, its __new__(cls...) static
method] to enable me to do the following?*
*    c=C(b)     # <<< This is what I want.*

    *# Note: I already know I can embed the B in my C class (aka "has-a"),
then I can avoid the trouble, *
*    # but ... doing c=C(b) is more fun and elegant.*

**
*Thanks,*
**
*Kenny*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060212/93be71a1/attachment.html 


More information about the Tutor mailing list