有没有一种方法可以将类实例传递给dunder init?

时间:2019-06-07 19:22:29

标签: python

“任务”类实例需要包含有关其先决任务的信息。

import typing

class Task:
    def __init__(
        self, 
        prerequisite: typing.Set(Task)
    ):
        self.prerequisite = prerequisite
File "test.py", line 3, in <module>
    class Task:
  File "test.py", line 6, in Task
    prerequisite: typing.Set(Task)
NameError: name 'Task' is not defined

1 个答案:

答案 0 :(得分:0)

您可以使用和抽象类

from abc import ABC
import typing

class TaskType(ABC):
    pass

class Task:
    def __init__(self, prerequisite: typing.Set(TaskType)):
        self.prerequisite = prerequisite