Elegant copy-by-value

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

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.


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


More information about the Python-list mailing list