2.3. SetView class

class immutable_views.SetView(a_set)[source]

An immutable set view.

Derived from Set.

This class provides an immutable view on a possibly mutable set object. The set object must be an instance of Set.

This can be used for example when a class maintains a set that should be made available to users of the class without allowing them to modify the set.

In the description of this class, the term ‘view’ always refers to the SetView object, and the term ‘set’ or ‘original set’ refers to the set object the view is based on.

The SetView class supports the complete behavior of Python class set, except for any methods that would modify the set. Note that the non-modifying methods of class set are a superset of the methods defined for the abstract class Set (the methods are listed in the table at the top of the linked page).

The view is “live”: Since the view class delegates all operations to the original set, any modification of the original set object will be visible in the view object.

Note that only the view object is immutable, not necessarily its items. So if the items in the original set are mutable objects, they can be modified through the view.

Note that in Python, augmented assignment (e.g. += is not guaranteed to modify the left hand object in place, but can result in a new object. For details, see object.__iadd__(). The += operator on a left hand object that is a SetView object results in a new SetView object on a new set object.

Parameters

a_set (Set) – The original set. If this object is a SetView, its original set is used.

Methods:

__and__(other)

self & other: Return a new view on the intersection of the set and the other set.

__contains__(value)

value in self: Return a boolean indicating whether the set contains a value.

__eq__(other)

self == other: Return a boolean indicating whether the set is equal to the other set.

__ge__(other)

self >= other: Return a boolean indicating whether the set is an inclusive superset of the other set.

__gt__(other)

self > other: Return a boolean indicating whether the set is a proper superset of the other set.

__iter__()

iter(self) ...: Return an iterator through the set.

__le__(other)

self <= other: Return a boolean indicating whether the set is an inclusive subset of the other set.

__len__()

len(self): Return the number of items in the set.

__lt__(other)

self < other: Return a boolean indicating whether the set is a proper subset of the other set.

__ne__(other)

self != other: Return a boolean indicating whether the set is not equal to the other set.

__or__(other)

self | other: Return a new view on the union of the set and the other set.

__rand__(other)

other & self: Return a new view on the intersection of the set and the other set.

__repr__()

repr(self): Return a string representation of the view suitable for debugging.

__ror__(other)

other | self: Return a new view on the union of the set and the other set.

__rsub__(other)

other - self: Return a new view on the difference of the other set and the set.

__rxor__(other)

other ^ self: Return a new view on the symmetric difference of the set and the other set.

__sub__(other)

self - other: Return a new view on the difference of the set and the other set.

__xor__(other)

self ^ other: Return a new view on the symmetric difference of the set and the other set.

copy()

Return a new view on a shallow copy of the set.

difference(*others)

Return a new view on the difference of the set and the other iterables.

intersection(*others)

Return a new view on the intersection of the set and the other iterables.

isdisjoint(other)

Return a boolean indicating whether the set does not intersect with the other iterable.

issubset(other)

Return a boolean indicating whether the set is an inclusive subset of the other iterable.

issuperset(other)

Return a boolean indicating whether the set is an inclusive superset of the other iterable.

symmetric_difference(other)

Return a new view on the symmetric difference of the set and the other iterable.

union(*others)

Return a new view on the union of the set and the other iterables.

__and__(other)[source]

self & other: Return a new view on the intersection of the set and the other set.

The returned SetView object is a view on a new set object of the type of the left hand operand that contains the items that are in the original set of the left hand operand and in the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__contains__(value)[source]

value in self: Return a boolean indicating whether the set contains a value.

The return value indicates whether the original set contains an item that is equal to the value.

__eq__(other)[source]

self == other: Return a boolean indicating whether the set is equal to the other set.

The return value indicates whether the items in the original set are equal to the items in the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

Raises

TypeError – The other object is not a set or SetView.

__ge__(other)[source]

self >= other: Return a boolean indicating whether the set is an inclusive superset of the other set.

The return value indicates whether every item in the other set (or in case of a SetView, its original set) is in the original set.

The other object must be a set or SetView.

Raises

TypeError – The other object is not a set or SetView.

__gt__(other)[source]

self > other: Return a boolean indicating whether the set is a proper superset of the other set.

The return value indicates whether the original set is a proper superset of the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

Raises

TypeError – The other object is not a set or SetView.

__iter__()[source]

iter(self) ...: Return an iterator through the set.

The returned iterator yields the items in the original set in its iteration order.

__le__(other)[source]

self <= other: Return a boolean indicating whether the set is an inclusive subset of the other set.

The return value indicates whether every item in the original set is in the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

Raises

TypeError – The other object is not a set or SetView.

__len__()[source]

len(self): Return the number of items in the set.

The return value is the number of items in the original set.

__lt__(other)[source]

self < other: Return a boolean indicating whether the set is a proper subset of the other set.

The return value indicates whether the original set is a proper subset of the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

Raises

TypeError – The other object is not a set or SetView.

__ne__(other)[source]

self != other: Return a boolean indicating whether the set is not equal to the other set.

The return value indicates whether the items in the original set are not equal to the items in the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

Raises

TypeError – The other object is not a set or SetView.

__or__(other)[source]

self | other: Return a new view on the union of the set and the other set.

The returned SetView object is a view on a new set object of the type of the left hand operand that contains all the (unique) items from the original set of the left hand operand and the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__rand__(other)[source]

other & self: Return a new view on the intersection of the set and the other set.

This method is a fallback and is called only if the left operand does not support the operation.

The returned SetView object is a view on a new set object of the type of the right hand operand that contains the items that are in the original set of the right hand operand and in the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__repr__()[source]

repr(self): Return a string representation of the view suitable for debugging.

The original set is represented using its repr() representation.

__ror__(other)[source]

other | self: Return a new view on the union of the set and the other set.

This method is a fallback and is called only if the left operand does not support the operation.

The returned SetView object is a view on a new set object of the type of the right hand operand that contains all the (unique) items from the original set of the right hand operand and the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__rsub__(other)[source]

other - self: Return a new view on the difference of the other set and the set.

This method is a fallback and is called only if the left operand does not support the operation.

The returned SetView object is a view on a new set object of the type of the left hand operand that contains the items that are in the other set (or in case of a SetView, its original set) but not in the original set of the left hand operand.

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__rxor__(other)[source]

other ^ self: Return a new view on the symmetric difference of the set and the other set.

This method is a fallback and is called only if the left operand does not support the operation.

The returned SetView object is a view on a new set object of the type of the right hand operand that contains the items that are in either the original set of the right hand operand or in the other set (or in case of a SetView, its original set), but not in both.

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__sub__(other)[source]

self - other: Return a new view on the difference of the set and the other set.

The returned SetView object is a view on a new set object of the type of the left hand operand that contains the items that are in the original set of the left hand operand but not in the other set (or in case of a SetView, its original set).

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

__xor__(other)[source]

self ^ other: Return a new view on the symmetric difference of the set and the other set.

The returned SetView object is a view on a new set object of the type of the left hand operand that contains the items that are in either the original set of the left hand operand or in the other set (or in case of a SetView, its original set), but not in both.

The other object must be a set or SetView.

The set and the other set are not changed.

Raises

TypeError – The other object is not a set or SetView.

copy()[source]

Return a new view on a shallow copy of the set.

The returned SetView object is a new view object on a set object of the type of the original set.

If the set type is immutable, the returned set object may be the original set object. If the set type is mutable, the returned set is a new set object that is a shallow copy of the original set object.

difference(*others)[source]

Return a new view on the difference of the set and the other iterables.

The returned SetView object is a view on a new set object of the type of the original set that contains the items that are in the original set but not in any of the other iterables (or in case of SetView objects, their original sets).

The other objects must be iterables.

The set and the other iterables are not changed.

Raises

TypeError – The other objects are not all iterables.

intersection(*others)[source]

Return a new view on the intersection of the set and the other iterables.

The returned SetView object is a view on a new set object of the type of the original set that contains the items that are in the original set and in the other iterables (or in case of SetView objects, their original sets).

The other objects must be iterables.

The set and the other iterables are not changed.

Raises

TypeError – The other objects are not all iterables.

isdisjoint(other)[source]

Return a boolean indicating whether the set does not intersect with the other iterable.

The return value indicates whether the original set has no items in common with the other iterable (or in case of a SetView, its original set).

The other object must be an iterable.

Raises

TypeError – The other object is not an iterable.

issubset(other)[source]

Return a boolean indicating whether the set is an inclusive subset of the other iterable.

The return value indicates whether every item in the original set is in the other iterable (or in case of a SetView, its original set).

The other object must be an iterable.

Raises

TypeError – The other object is not an iterable.

issuperset(other)[source]

Return a boolean indicating whether the set is an inclusive superset of the other iterable.

The return value indicates whether every item in the other iterable (or in case of a SetView, its original set) is in the original set.

The other object must be an iterable.

Raises

TypeError – The other object is not an iterable.

symmetric_difference(other)[source]

Return a new view on the symmetric difference of the set and the other iterable.

The returned SetView object is a view on a new set object of the type of the original set that contains the items that are in either the original set or in the other iterable (or in case of a SetView, its original set), but not in both.

The other object must be an iterable.

The set and the other iterable are not changed.

Raises

TypeError – The other object is not an iterable.

union(*others)[source]

Return a new view on the union of the set and the other iterables.

The returned SetView object is a view on a new set object of the type of the original set that contains all the (unique) items from the original set and the other iterables (or in case of SetView objects, their original sets).

The other objects must be iterables.

The set and the other iterables are not changed.

Raises

TypeError – The other objects are not all iterables.