Copy construction of class instance object

Gerrit Holl gerrit at nl.linux.org
Tue May 27 17:20:01 EDT 2003


Bror Johansson schreef op dinsdag 27 mei om 22:41:15 +0000:
> import copy
> 
> class A(object):
>     def __init__(self, ainst):
>         self.val = 5
> 
> class B(A):
>     def __init__(self, ainst):
>         self = copy.deepcopy(ainst)

You are assiging a new value to the name 'self' here. 'self' is
not special. When B is initiateted, __init__ is called and to
the name 'self', the object is bound. When you assign something
new to self, this does not change the object.

You can try:

self.__dict__ = ainst.__dict__ # WARNING: not tested!

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

yours,
Gerrit.

-- 
100. ... interest for the money, as much as he has received, he shall
give a note therefor, and on the day, when they settle, pay to the
merchant. 
        -- Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list