Created
April 3, 2025 14:19
-
-
Save DavesCodeMusings/ce25c1c2cff22dd5228e7c22cb777ade to your computer and use it in GitHub Desktop.
An 8-bit number as a class property that increments itself each time it's read.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Auto-incrementing property | |
| class Test: | |
| _packet_id = -1 | |
| @property | |
| def packet_id(self): | |
| self._packet_id += 1 | |
| self._packet_id &= 0xFF # Truncate to 8-bit max | |
| return self._packet_id | |
| def demo(): | |
| test = Test() | |
| for i in range(0, 260): | |
| print(getattr(test, "packet_id")) | |
| if __name__ == "__main__": | |
| demo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment