Passing self into a constructor in python?

Yes it is legal, and yes it is pythonic I find myself using this pattern when you have an object and a container object where the contained objects need to know about their parent.

Yes it is legal, and yes it is pythonic. I find myself using this pattern when you have an object and a container object where the contained objects need to know about their parent.

Yeah I understand why you need to do it, but I'm just not sure why I haven't seen it done very much in python. – Falmarri Oct 15 '10 at 20:40 1 Possibly because python has a wicked set of polymorphic container types which in general are sufficient for all your needs? – Nick Craig-Wood Oct 15 '10 at 20:44 Can adapter pattern be considered one of the uses, I mean if we don't consider parameter to be passed as self but another object instance which has to be contained.

– Ashish Oct 15 '10 at 21:14.

Just pass it like a parameter. Of course, it won't be called self in the other initializer... class A: def __init__(self, num, target): self. Num = num self.

Target = target class B: def __init__(self, num): self. A = A(num, self) a = A(1) be = B(2) print b.a. Num # prints 2.

I think this is more or less asking whether its pythonic, not how. – monadic Oct 15 '10 at 20:48.

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