I've searched a lot, but what I find is mainly examples of recursive programming in python. So here goes the question:
我搜索了很多,但我发现的主要是python中的递归编程示例。所以问题如下:
How can I achieve this?
我怎样才能做到这一点?
class A:
b = B()
class B:
a = A()
1 个解决方案
#1
8
Everything is dynamic in Python - even the class declarations. There's nothing to stop you modifying the contents of a class after the initial declaration:
Python中的一切都是动态的 - 甚至是类声明。在初始声明之后,没有什么可以阻止你修改类的内容:
class A:
pass
class B:
a = A()
A.b = B()
NB: If you're not that familiar with Python, the pass
keyword simply allows you to say 'nothing here' - it's not important unless class A is as empty as it is in this example!
注意:如果你不熟悉Python,那么pass关键字只允许你说“没有在这里” - 除非A类在这个例子中是空的,否则它并不重要!
#1
8
Everything is dynamic in Python - even the class declarations. There's nothing to stop you modifying the contents of a class after the initial declaration:
Python中的一切都是动态的 - 甚至是类声明。在初始声明之后,没有什么可以阻止你修改类的内容:
class A:
pass
class B:
a = A()
A.b = B()
NB: If you're not that familiar with Python, the pass
keyword simply allows you to say 'nothing here' - it's not important unless class A is as empty as it is in this example!
注意:如果你不熟悉Python,那么pass关键字只允许你说“没有在这里” - 除非A类在这个例子中是空的,否则它并不重要!