Mailing List Archive

Recursive constructors?
Is it legal for a constructor function to call itself recursively? I.e.:

class foo:
def __init__(self, x):
if (whatever):
self.bar = foo(y)

--
Roy Smith <roy@popmail.med.nyu.edu>
New York University School of Medicine
Recursive constructors? [ In reply to ]
Of course it is ok to call recursively. self.bar will contain a different
instance of class foo. Never had to write this, but shouldn't be any kind
of problem. Make sure you have initialized the class first of course.

-----Original Message-----
From: roy@endeavor.med.nyu.edu [mailto:roy@endeavor.med.nyu.edu]
Sent: Friday, July 30, 1999 12:18 PM
To: python-list@cwi.nl
Subject: Recursive constructors?


Is it legal for a constructor function to call itself recursively? I.e.:

class foo:
def __init__(self, x):
if (whatever):
self.bar = foo(y)

--
Roy Smith <roy@popmail.med.nyu.edu>
New York University School of Medicine
Recursive constructors? [ In reply to ]
Roy Smith <roy@popmail.med.nyu.edu> wrote:
> Is it legal for a constructor function to call itself recursively? I.e.:
>
> class foo:
> def __init__(self, x):
> if (whatever):
> self.bar = foo(y)

why not just try it? ;-)

(hint: the class statement will finish before
you get around to actually create an in-
stance of the class, and at that time, 'foo'
is already in the GLOBAL namespace).

</F>
Recursive constructors? [ In reply to ]
It is legal, but you had better make sure that (whatever) becomes false
somewhere along the line, as a side-effect of foo::__init__. Otherwise, I
pity the foo at the bottom, who has to carry an entire heap full of bars.
(okay,okay)

>class foo:
> def __init__(self, x):
> if (whatever):
> self.bar = foo(y)