[Tutor] Re: Independent class instances

Derrick 'dman' Hudson dman@dman.ddts.net
Wed Nov 27 12:46:19 2002


--qDbXVdCdHGoSgWSk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Nov 27, 2002 at 11:54:42AM -0500, Runsun Pan wrote:
|=20
| Hi all,
|=20
| I have some class, say, myFont. I'm trying to make different
| instances outta it:
|=20
| f1 =3D myFont()
| f1.color =3D 'red'

| f2 =3D f1

Here you duplicate the _reference_.  Now the names 'f1' and 'f2' refer
to the exact same object in memory.

| f2.color =3D 'black'

| When I assign 'black' to f2.color, f1.color changed to 'black' too.

See above for the reason.

| This is not what I want.

| Is there anyway to 'copy' entire content of a class to a new=20
| instance ?

import copy
f2 =3D copy.deepcopy( f1 )

-D

--=20
All a man's ways seem innocent to him,
but motives are weighed by the Lord.
        Proverbs 16:2
=20
http://dman.ddts.net/~dman/

--qDbXVdCdHGoSgWSk
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj3lCDsACgkQO8l8XBKTpRQ7gACeKQi3sgdJmDdiGkgSrl5YBFzq
GMEAoMyVRKmaGghsdefGMTCtCIoBQZez
=CG2k
-----END PGP SIGNATURE-----

--qDbXVdCdHGoSgWSk--