Elegant copy-by-value

Martin Christensen knightsofspamalot-factotum at gvdnet.dk
Sun Jan 12 14:19:58 CET 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>>>>> "Carl" == Carl Banks <imbosol at vt.edu> writes:
Carl> For the types where it is most useful to do copy-by-value, there
Carl> are built-in ways to do shallow copies.  For example, lists (and
Carl> Numeric arrays) can do this:
Carl>     a = b[:]

Yup, that's one of the ones you pick up rather quickly. :-)

Numeric arrays? What did I miss?

Carl> Dicts have a copy method:
Carl>     a = b.copy()

Ah, I never noticed that one. Thanks, that'll come in handy some day,
I'm sure.

Carl> Class instances don't have such a method by default, seeing that
Carl> they can have more complicated state that might not work with a
Carl> simple copy.  However, it's easy to define a class that does, if
Carl> you need it.  For example, extending the Bunch class from Python
Carl> Cookbook: [...]

Nice example. For some things that might be the way to go.

Martin

- -- 
Homepage:       http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEUEARECAAYFAj4ha34ACgkQYu1fMmOQldVh3gCgxSUb8WzjuGGDOpbkbqeRCutL
+DwAl03Vjv32Nf/LRlY7AkAZhSEzkSc=
=RJ4W
-----END PGP SIGNATURE-----




More information about the Python-list mailing list