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
  • 179,855
  • 19
  • 132
  • 245
David542
  • 104,438
  • 178
  • 489
  • 842

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
  • 21,558
  • 8
  • 41
  • 88