def METHOD_NAME(self, last=True):
    if not self:
        raise KeyError("set is empty")
    if last:
        key = next(reversed(self))
    else:
        key = next(iter(self))
    self.discard(key)
    return key