我尝试使文件夹中的文件名包含类。但我希望它像集合一样行事。现在我有这样的:使自定义类的行为像集
class Files():
def __init__(self, in_dir):
self.in_dir = in_dir
self.files = set(map(os.path.basename, glob.glob(self.in_dir + "/*.txt")))
def __add__(self, other):
return self.files + other.files
def __or__(self, other):
return self.files | other.files
def __and__(self, other):
return self.files & other.files
def __xor__(self, other):
return self.files^other.files
这项工作,我可以这样做:
f1 = Files(inDir1)
f2 = Files(inDir2)
diff_files = f1^f2 % this give files that are in f1 or f2 folder but not in both folders
这是好的,但问题是,diff_files
不是Files
实例。如何改变我的类,像在python 3.x中设置一样?
任何你不使用'set'可怕原因ctly?我想有更多的代码没有被显示,但是现在我会把'__init__'中的代码作为一个函数返回一个集合。 –
这是一个小例子。我希望文件具有自定义功能,如删除self.files中的文件。 – user3654650