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
  • 126,858
  • 14
  • 92
  • 185
David542
  • 96,524
  • 132
  • 375
  • 637

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
  • 12,841
  • 5
  • 28
  • 69