""" Inconsistent behavior of set operators in Python 3.5.2. - Using set.intersection generally throws a TypeError exception if its inputs include something that is not a set. However, it correctly finds the intersection between a set and a tuple when the first input is a set and the second is a tuple. - Similar behavior occurs for multiple inputs, inputs that are lists or dicts (dict uses keys), and for other operators such as set.difference and set.union. - The equivalent operators (&, -, |) do not allow any non-set inputs. It appears that set.intersection, set.union, etc. only checks if its first input is a set. If so, other iterable objects can be used and a correct result is returned. """ # Exception when set.intersection sees inputs of tuple, set. my_set = {1, 2, 3} my_tuple = (2, 3, 4) my_other_tuple = (3, 4, 5) my_list = [2, 3, 4] my_dict = {2: "b", 3: "c", 4: "d"} print("My set: {}".format(my_set)) print("My tuple: {}".format(my_tuple)) print("My other tuple: {}".format(my_other_tuple)) print("My list: {}".format(my_list)) print("My dict: {}".format(my_dict)) print() # Behavior using intersection. print("Behavior using intersection:") print("tuple & set: ", end="") # raises TypeError exception. try: print(my_tuple & my_set) except TypeError: print("TypeError exception raised.") print("set & tuple: ", end="") # raises TypeError exception. try: print(my_set & my_tuple) except TypeError: print("TypeError exception raised.") print("set.intersection(tuple, set): ", end="") # raises TypeError exception. try: print(set.intersection(my_tuple, my_set)) except TypeError: print("TypeError exception raised.") print("set.intersection(set, tuple): ", end="") # returns valid intersection. try: print(set.intersection(my_set, my_tuple)) except TypeError: print("TypeError exception raised.") # Similar behavior using union and difference. print("\nSimilar behavior using union and difference:") print("set.union(tuple, set): ", end="") # raises TypeError exception. try: print(set.union(my_tuple, my_set)) except TypeError: print("TypeError exception raised.") print("set.union(set, tuple): ", end="") # returns valid intersection. try: print(set.union(my_set, my_tuple)) except TypeError: print("TypeError exception raised.") print("set.difference(tuple, set): ", end="") # raises TypeError exception. try: print(set.difference(my_tuple, my_set)) except TypeError: print("TypeError exception raised.") print("set.difference(set, tuple): ", end="") # returns valid intersection. try: print(set.difference(my_set, my_tuple)) except TypeError: print("TypeError exception raised.") # Behavior using 3 inputs to intersection. print("\nBehavior using 3 inputs to intersection:") print("set.intersection(tuple, set, tuple): ", end="") # Raises TypeError exception. try: print(set.intersection(my_tuple, my_set, my_other_tuple)) except TypeError: print("TypeError exception raised.") print("set.intersection(tuple, tuple, set): ", end="") # Raises TypeError exception. try: print(set.intersection(my_tuple, my_other_tuple, my_set)) except TypeError: print("TypeError exception raised.") print("set.intersection(set, tuple, tuple): ", end="") # Returns valid intersection. try: print(set.intersection(my_set, my_tuple, my_other_tuple)) except TypeError: print("TypeError exception raised.") # Behavior using list and dict. print("\nBehavior using list and dict:") print("set.intersection(list, set): ", end="") # raises TypeError exception. try: print(set.intersection(my_list, my_set)) except TypeError: print("TypeError exception raised.") print("set.intersection(set, list): ", end="") # returns valid intersection. try: print(set.intersection(my_set, my_list)) except TypeError: print("TypeError exception raised.") print("set.intersection(dict, set): ", end="") # raises TypeError exception. try: print(set.intersection(my_dict, my_set)) except TypeError: print("TypeError exception raised.") print("set.intersection(set, dict): ", end="") # returns valid intersection. try: print(set.intersection(my_set, my_dict)) except TypeError: print("TypeError exception raised.")