0

Is there an equivalent of the following in python?

ICMP_SEQUENCE_NUM   = self.sequence_num ++

That is, to do assignICMP_SEQUENCE_NUM = self.sequence_num, and after that, increment self.sequence_num by one?

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
David542
  • 101,766
  • 154
  • 423
  • 727

1 Answers1

3

Although there is no way to perform a postfix or prefix operation directly, you can use the new walrus operator := (assignment expression) to get close. This is only possible in Python >= 3.8:

# works
self.sequence_num = (ICMP_SEQUENCE_NUM := self.sequence_num) + 1

Note that you can't use the walrus operator on object attributes, so something like the following is not possible

# does not work
ICMP_SEQUENCE_NUM = (self.sequence_num := self.sequence_num + 1) - 1
flakes
  • 17,121
  • 8
  • 34
  • 75