Proposal: Introduce .= operator for In-Place Method Invocation

Motivation:

In Python, many objects, especially immutable ones like strings, require reassigning variables after method calls:

string = string.replace("o", "x")

While functional, this pattern involves repeated references to the same variable, which can be verbose and error-prone. The proposed .= operator would allow for a more succinct expression:

string .= replace("o", "x")

This change aligns with Python’s emphasis on readability and expressiveness, particularly for scenarios involving chaining multiple transformations.

This maynot be that usefull with string because we can just do this:

string = string.replace(..., ...).lower().strip()

But I can see this proving very usefull

object = object.method(args)
# Will become
object .= method(args)
1 Like

This has already been discussed and rejected:

But I’ll forgive you, as Discourse doesn’t allow you to search for .=.

2 Likes