Skip to content Skip to sidebar Skip to footer

Python Circular References

trying to have two class that reference each others, in the same file. What would be the best way to have this working: class Foo(object): other = Bar class Bar(object): o

Solution 1:

This would do what you want:

classFoo(object):
    pass

classBar(object):
    pass

Foo.other = Bar
Bar.other = Foo

I would prefer to avoid such design completely, though.

Solution 2:

Assuming that you really want Foo.other and Bar.other to be class properties, rather than instance properties, then this works (I tested, just to be sure) :

classFoo(object):
    pass

classBar(object):
    pass

Foo.other = Bar
Bar.other = Foo

If it's instance properties that you're after, then aaronasterling's answer is more appropriate.

Post a Comment for "Python Circular References"