The walrus operator :=
(also known as "assignment expression") should be used
with caution as it can easily make code more difficult to understand and thus maintain. In such case it is advised to refactor the code and use an
assignment statement (i.e. =
) instead.
Reasons why it is better to avoid using the walrus operator in Python:
- Readability: The walrus operator can lead to more complex and nested expressions, which might reduce the readability of the
code, especially for developers who are not familiar with this feature.
- Compatibility: If you are working on projects that need to be compatible with older versions of Python (before 3.8), you
should avoid using the walrus operator, as it won’t be available in those versions.