使自定义类的行为像set [英] Making custom class to behave like set

查看:125
本文介绍了使自定义类的行为像set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在文件夹中创建一个包含文件名的类。但我希望它的行为像设置。现在我有这个:

  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%这给出f1或f2文件夹中但不在两个文件夹中的文件

这是确定,但问题是 diff_files 不是文件。如何改变我的类,像在python 3.x中设置一样?

解决方案 in_dir 参数可选:

  def __init __(self,in_dir = None):
if in_dir:
self.in_dir = in_dir
self.files = set(map(os.path.basename,glob.glob(self.in_dir +/*.txt)))



然后,更改 __ xor __()

  def __xor __(self,other):
instance = Files()
instance.files = self.files ^ other .files
return instance

此外,我没有看到保留 in_dir 作为实例变量。您可以简化 __ init __()

  def __init __如果in_dir:
self.files = set(map(os.path.basename,glob.glob(in_dir +/*.txt)))






或者,您可以允许初始化 Files 通过传递文件设置:

  def __init __(self,in_dir = None,files = None):
如果in_dir:
self.files = set(map(os.path.basename,glob.glob(in_dir +/*.txt )))
如果文件:
self.files = files

__ xor __()方法更简单:

  def __xor __ self,other):
return Files(files = self.files ^ other.files)


I try to make a class containing file names in a folder. But I want it to behave like set. Now I have this:

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

This work and I can do like this:

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

This is ok, but the problem is that diff_files is not instance of Files. How to change my class, to behave like set in python 3.x?

解决方案

First, make in_dir argument optional:

def __init__(self, in_dir=None):
    if in_dir:
        self.in_dir = in_dir
        self.files = set(map(os.path.basename, glob.glob(self.in_dir + "/*.txt")))

Then, change the __xor__():

def __xor__(self, other):
    instance = Files()
    instance.files = self.files ^ other.files
    return instance

Also, I don't see the reason to keep in_dir as an instance variable. You can simplify the __init__():

def __init__(self, in_dir=None):
    if in_dir:
        self.files = set(map(os.path.basename, glob.glob(in_dir + "/*.txt")))


Alternatively, you can allow to initialize Files by passing a files set:

def __init__(self, in_dir=None, files=None):
    if in_dir:
        self.files = set(map(os.path.basename, glob.glob(in_dir + "/*.txt")))
    if files:
        self.files = files

Then, the __xor__() method would be even simpler:

def __xor__(self, other):
    return Files(files=self.files ^ other.files)

这篇关于使自定义类的行为像set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆