Skip to content

Instantly share code, notes, and snippets.

@DavesCodeMusings
Created April 3, 2025 14:19
Show Gist options
  • Select an option

  • Save DavesCodeMusings/ce25c1c2cff22dd5228e7c22cb777ade to your computer and use it in GitHub Desktop.

Select an option

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.
# 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