Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python3x中两个子类之间的跨类引用_Python 3.x - Fatal编程技术网

Python 3.x python3x中两个子类之间的跨类引用

Python 3.x python3x中两个子类之间的跨类引用,python-3.x,Python 3.x,有人能帮助理解如何从python3中的另一个子类生成子类构造函数吗??? 我可能需要进行一些类的preinit导入? 所有类都在一个文件中,因此我无法导入它 class Parent(): def __init__(self): print "Parent" class ChildOne(Parent): two = ChildTwo() # Not works!!! How to make it works??? def __init__(self)

有人能帮助理解如何从python3中的另一个子类生成子类构造函数吗??? 我可能需要进行一些类的preinit导入? 所有类都在一个文件中,因此我无法导入它

class Parent():
    def __init__(self):
        print "Parent"

class ChildOne(Parent):
    two = ChildTwo() # Not works!!! How to make it works???

    def __init__(self):
        Parent.__init__(self)
        two.print_two()

    def print_one(self):
        print "One"

class ChildTwo(Parent):
    one = ChildOne() # This works!!!

    def __init__(self):
        Parent.__init__(self)
        one.print_one()

    def print_two(self):
        print "Two"


没有办法做到这一点,因为子类之间存在递归。 唯一的方法是将这个print_one和print_two移动到父类并从父类使用它