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))
I'm using Visual Studio code with PyLint
and it returns an error with the copy_files_with
method.
line 20, in Folder
def copy_files_with(self, extension: str, to_location: Folder)
I removed all the unnecessary code, but line 20 is where the method copy_files_with
is located.
I don't understand why, the __eq__
method can see the Folder
class in the isinstance
call. I want the to_location
to be a Folder
, and I want to specify that in the type hint, how do I do this?
When a class and its methods are processed, in the moment a module is imported, the lines containing the "class" and "def" statements are run. The method bodies - the code inside the "def" blocks, are only run when the class is later instantiated and have its methods called.
So, it is natural that a class is not defined inside its own body - the class body have to be processed before Python will create the class itself - and only then it will be associated with its name.
The way to make this work - references to the own class as well as refrences to class that are forward in the same file, is to use their name as strings in the annotations - in this case, you should use
def copy_files_with(self, extension: str, to_location: 'Folder'):
and it should work with most tools that make use of the annotations.