You can click it later:
class class1(object): def __init__(self, friend=None): if friend is None: friend = class2 self.friendInstance = friend()
Edit: Actually, do not do this. It will create an instance of class2, which creates an instance of class1, which creates an instance of class2, etc. Perhaps you really want to pass an instance instead of the class you are creating:
class class1(object): def __init__(self, friend=None): if friend is None: self.friendInstance = class2(self) else: self.friendInstance = friend
and similarly for class 2. It's not that flexible, but it's pretty simple. If you really need flexibility, you can do something like this:
class class1(object): def __init__(self, friend=None, friendClass=None): if friend is None: self.friendInstance = (class2 if friendClass is None else friendClass)(self) else: self.friendInstance = friend class class2(object): def __init__(self, friend=None, friendClass=class1): if friend is None: self.friendInstance = friendClass(self) else: self.friendInstance = friend
This may be simplified with inheritance or metaclasses, but you are likely to get this idea.
source share