class initialization problem

Carl Banks pavlovevidence at gmail.com
Fri Sep 18 00:54:26 EDT 2009


On Sep 17, 8:27 pm, rantingrick <rantingr... at gmail.com> wrote:
> ok i have a class and in it's constructor i want to create a copy of
> it as an attribute, i have tried super, __new__, and noting seems to
> work, please help!
>
> class A(base):
>     def __init__(self):
>         super(A, self).__init__()
>         self.nested = ?
>
> think of a nested list [ [] ] but with object "A" as the toplevel list
> and having an instance of A in the attribute "nested"

Well, to answer the question you asked ("i have a class and in it's
constructor i want to create a copy of it as an attribute"):

import copy

self.nested = copy.copy(self)


However, your post contains some conflicting information, "copy" often
means different things to different people, "it" is ambiguous, and
what you ask for seems to to be well-conceived.  I think we will be
able to help you more if you give more details about what you expect
and how you intend to use this nested object.

Please try to observe the distiction between classes and instances
(you almost certainly wanted a copy of the instance, not of the
class).


Carl Banks



More information about the Python-list mailing list