Class 方法参数相同时未定义名称 class
Class name isn't defined when method parameter is of the same class
class Folder:
def copy_files_with(self, extension: str, to_location: Folder):
pass
def __eq__(self, other):
if isinstance(other, Folder):
return (self._parent, self._subdirectory) == (other._parent, other._subdirectory)
return NotImplemented
def __hash__(self):
return hash((self._parent, self._subdirectory))
我将 Visual Studio 代码与 PyLint
一起使用,returns copy_files_with
方法出错。
line 20, in Folder
def copy_files_with(self, extension: str, to_location: Folder)
我删除了所有不需要的代码,但是第20行是方法copy_files_with
所在的地方。
不明白为什么,__eq__
方法可以在isinstance
调用中看到Folder
class。我希望 to_location
成为 Folder
,我想在类型提示中指定它,我该怎么做?
当 class 及其方法被处理时,在导入模块的那一刻,包含 "class" 和 "def" 语句的行是 运行。方法体 - "def" 块内的代码,只有 运行 当 class 稍后实例化并调用其方法时。
因此,class 没有在 内部定义 是很自然的 - class 主体必须在 [=24] 之前处理=] 将创建 class 本身 - 只有这样它才会与其名称相关联。
实现此功能的方法 - 在同一文件中引用自己的 class 以及对 class 的引用,是在注释中使用它们的名称作为字符串 -在这种情况下,您应该使用
def copy_files_with(self, extension: str, to_location: 'Folder'):
它应该适用于大多数使用注释的工具。
class Folder:
def copy_files_with(self, extension: str, to_location: Folder):
pass
def __eq__(self, other):
if isinstance(other, Folder):
return (self._parent, self._subdirectory) == (other._parent, other._subdirectory)
return NotImplemented
def __hash__(self):
return hash((self._parent, self._subdirectory))
我将 Visual Studio 代码与 PyLint
一起使用,returns copy_files_with
方法出错。
line 20, in Folder
def copy_files_with(self, extension: str, to_location: Folder)
我删除了所有不需要的代码,但是第20行是方法copy_files_with
所在的地方。
不明白为什么,__eq__
方法可以在isinstance
调用中看到Folder
class。我希望 to_location
成为 Folder
,我想在类型提示中指定它,我该怎么做?
当 class 及其方法被处理时,在导入模块的那一刻,包含 "class" 和 "def" 语句的行是 运行。方法体 - "def" 块内的代码,只有 运行 当 class 稍后实例化并调用其方法时。
因此,class 没有在 内部定义 是很自然的 - class 主体必须在 [=24] 之前处理=] 将创建 class 本身 - 只有这样它才会与其名称相关联。
实现此功能的方法 - 在同一文件中引用自己的 class 以及对 class 的引用,是在注释中使用它们的名称作为字符串 -在这种情况下,您应该使用
def copy_files_with(self, extension: str, to_location: 'Folder'):
它应该适用于大多数使用注释的工具。