Create instance of a class in the same class's definition in python?

Well, it fails because it has infinite recursion. Think about it, if every MyClass has a child which is a MyClass, it will go on for infinity!

Well, it fails because it has infinite recursion. Think about it, if every MyClass has a child which is a MyClass, it will go on for infinity! You can resolve this a couple of ways.

First, you can have a parameter to the constructor: class MyClass: def __init__(self, create = True): if create: self. Child = MyClass(False) mc = MyClass() Or, you can have another, external method: class MyClass: def set_child(self,child = None): # I prefer to make child optional for ease of use. Child = MyClass() if child is None else child self.

Child=child mc=MyClass() mc. Set_child() I personally prefer the first solution as it means that outside objects don't need to know anything about the class. Of course, you could combine the two: class MyClass: def __init__(self, create): if create: self.

Set_child(create=False) def set_child(self,child = None, create = True): child = MyClass(create) if child is None else child self. Child=child mc=MyClass() This way mc has a child by default and you have the option of setting the child whenever you like. Then there is also the "let's create a certain number" approach: class MyClass: def __init__(self, count = 10): count -= 1 if count: # the first child gets the value 9.

# the second gets 8. # when the count gets to 0, stop! Self.

Child = MyClass(count) Aside: If you want to get an object's class, you can use the value obj. __class__. That will output MyClass in all of the examples above.

Quite comprehensive and illustrative. – xiaohan2012 Sep 2 '11 at 14:33.

You're making an infinitely recursing call — MyClass is creating another MyClass during initialization, and thus it recurses infinitely. You may want to do something like: class MyClass: def create_child(self): self. Child=MyClass() mc=MyClass() mc.

Create_child() If you're feeling particularly naughty, you could try: class MyClass(object): @property def child(self): if self. _child is None: self. _child = MyClass() return self.

_child def __init__(self): self. _child=None mc=MyClass().

MyClass is creating another MyClass. I do not quite understand it. Can you explain it more?

The code you provide is useful, thanks. :-) – xiaohan2012 Sep 2 '11 at 12:47 Oh, it makes sense to me now. :-) – xiaohan2012 Sep 2 '11 at 12:48 Asides, can you tell what the usage of @property is called?Thanks.

It is new to me. – xiaohan2012 Sep 2 '11 at 12:58 It is a 'decorator': ibm.com/developerworks/linux/library/l-c... – Don Sep 2 '11 at 15:21.

What you did there is actualy recursive, the new isntance of MyClass will create a new instance that will in turn create a new one, etc ... Soo I supose that is why your code fails, I can't tell for sure since you didn't post the error message.

I suggest to define two classes: class MyClass(object): def __init__(self): self. Child = MyChildClass() ...many other methods... class MyChildClass(MyClass): def __init__(self): pass I think that if two classes must behave in two different ways, they must be different (although one can subclass the other).

This seems precarious and unnecessary to me. – Mike Graham Sep 2 '11 at 14:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions