[Tutor] basic Qs [copy module]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri Feb 14 10:47:04 2003


> > how to create new objects from other object ???
> >
> > something like new instance with data of other object.  ( is object is
> > right word to use )
> >
> Ok you want a *copy* of a, right? A different object but with equal
> contents. There are various ways. Here goes the simplest:
>
> b = list(a)
>
> Here goes another:
>
> b = []
> for elem in a:
>     b.append(elem)

There's also a module meant to make copies of a Python object: it's the
'copy' module:

    http://www.python.org/doc/lib/module-copy.html

The functions in the 'copy' module provide a consistant way of copying an
object, and works on class instances as well as lists.

Hope this helps!