Add a new token to force the methods to returns the previous object

It’s quite common for a function to create an object and modify the object in some ways before returning the object, e.g.:

which, with the syntax I suggested above, can be rewritten as:

def __ror__(self, other):
    if isinstance(other, _collections_abc.Mapping):
        return self.__class__(){.update(other), .update(self)}
    return NotImplemented

An object can also go through various validations before proceeding with the main task:

which can be rewritten as:

def peek(self, n):
    return self{._check_not_closed(), ._check_read("peek")}._buffer.peek(n)

Like the walrus operator, this is a quality-of-life improvement that, while yes, one can certainly live without, can make such common patterns of codes handier to write.

4 Likes