Copy construction of class instance object

Bror Johansson bjohan at telia.com
Tue May 27 16:30:00 EDT 2003


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3ED3C1DE.8EC82FC2 at engcorp.com...
> Bror Johansson wrote:
> >
> > Is there a good/recommended way to emulate the copy constructor
> > classinstance creation (a la C++) in Python?
>
> "import copy" plus an appropriate line or two?  See
> http://www.python.org/doc/2.0/lib/module-copy.html

My try:

import copy

class A(object):
    def __init__(self, ainst):
        self.val = 5

class B(A):
    def __init__(self, ainst):
        self = copy.deepcopy(ainst)

a = A()
b= B(a)

print b.val

=> AttributeError: 'B' object has no attribute 'val'






More information about the Python-list mailing list