通过键入 a+b 来实现集合并集,其中 a 和 b 是两个字典

Cha*_*air 5 python-2.7

我想使用python中的类和字典来实现python中设置的内置数据类型。我已经包含了某些基本函数,但我无法执行在其上定义的并集和交集操作。我只想写 c=a+b 其中 a 和 b 是两个字典 c 是另一个字典,其键给出了 'a' 和 'b' 的并集。我尝试了 try 和 except 在我下面的代码中给出的,但我想要一个更好的解决方案。谁能帮我这个?

class My_Set:
      def  __init__(self,listt):
            if listt:
                    self.dictionary={}
                    i=0
                    for x in listt:
                            self.dictionary[x]=len(x)
                            i=i+1
            else:
                    self.dictionary={}

    def is_element(self,element):
            if element in self.dictionary:
                    return True
            else:
                    return False

    def remove(self,element):
            if element in self.dictionary:
                    self.dictionary.pop(element)
            else:
                    print 'element missing'
    def add_element(self,element):
            self.dictionary.update({element:len(element)})
            #return self.dictionary
    def union(self,other):
            self.dictionary.update(other.dictionary)
            return self.dictionary.keys()
Run Code Online (Sandbox Code Playgroud)