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, e.g. set, or a user-defined class.

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 ‘underlying 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 underlying set, any modification of the underlying 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 underlying set are mutable objects, they can be modified through the view.

Note that in Python, augmented assignment (e.g. x += y) is not guaranteed to modify the left hand object in place, but can result in the left hand name being bound to a new object (like in x = x + y). For details, see object.__iadd__().

For the SetView class, augmented assignment is supported and results in binding the left hand name to a new SetView object.

Parameters

a_set (Set) – The underlying set. If this object is a SetView, its underlying 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.

__getstate__()

Support for pickling.

__gt__(other)

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

__hash__()

hash(self): Return a hash value for the 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.

__setstate__(a_dict)

Support for unpickling.

__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.

Attributes:

set

The underlying set.

__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 underlying set of the left hand operand and in the other set (or in case of a SetView, its underlying 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 underlying 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 underlying set are equal to the items in the other set (or in case of a SetView, its underlying 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 underlying set) is in the underlying set.

The other object must be a set or SetView.

Raises

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

__getstate__()[source]

Support for pickling.

__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 underlying set is a proper superset of the other set (or in case of a SetView, its underlying set).

The other object must be a set or SetView.

Raises

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

__hash__()[source]

hash(self): Return a hash value for the set.

Whether hashing is supported depends on the underlying set. For example, the standard Python set class does not support hashing, but the standard Python frozenset class does.

Raises

TypeError – The underlying set does not support hashing.

__iter__()[source]

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

The returned iterator yields the items in the underlying 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 underlying set is in the other set (or in case of a SetView, its underlying 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 underlying 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 underlying set is a proper subset of the other set (or in case of a SetView, its underlying 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 underlying set are not equal to the items in the other set (or in case of a SetView, its underlying 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 underlying set of the left hand operand and the other set (or in case of a SetView, its underlying 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 underlying set of the right hand operand and in the other set (or in case of a SetView, its underlying 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 underlying 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 underlying set of the right hand operand and the other set (or in case of a SetView, its underlying 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 underlying set) but not in the underlying 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 underlying set of the right hand operand or in the other set (or in case of a SetView, its underlying 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.

__setstate__(a_dict)[source]

Support for unpickling.

__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 underlying set of the left hand operand but not in the other set (or in case of a SetView, its underlying 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 underlying set of the left hand operand or in the other set (or in case of a SetView, its underlying 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 underlying set.

If the set type is immutable, the returned set object may be the underlying set object. If the set type is mutable, the returned set is a new set object that is a shallow copy of the underlying 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 underlying set that contains the items that are in the underlying set but not in any of the other iterables (or in case of SetView objects, their underlying 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 underlying set that contains the items that are in the underlying set and in the other iterables (or in case of SetView objects, their underlying 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 underlying set has no items in common with the other iterable (or in case of a SetView, its underlying 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 underlying set is in the other iterable (or in case of a SetView, its underlying 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 underlying set) is in the underlying set.

The other object must be an iterable.

Raises

TypeError – The other object is not an iterable.

property set

The underlying set.

Access to the underlying set is provided for purposes such as conversion to JSON or other cases where the view classes do not work. This access should not be used to modify the underlying set.

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 underlying set that contains the items that are in either the underlying set or in the other iterable (or in case of a SetView, its underlying 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 underlying set that contains all the (unique) items from the underlying set and the other iterables (or in case of SetView objects, their underlying 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.