'Type overflow management

I need to convert a random number into a bytes class, with no error but a saturation if it is bigger than 255. I did:

a = 3250 
try:
    b = bytes([a]) 
except:
    b = bytes([255])

But it seems really complex. Is there a better way to do that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source