class initialization problem

rantingrick rantingrick at gmail.com
Fri Sep 18 01:08:55 EDT 2009


On Sep 17, 11:54 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> 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

ok here is some code. this will cause an infinite recursion.

class A():
    def __init__(self, *args):
        self.nestedA = A(*args) #NO GOOD!

there must be a way to create an instance of an object within the same
objects constructor?

TIA



More information about the Python-list mailing list