r/badcode • • Feb 16 '21

[deleted by user]

[removed]

31 Upvotes

77 comments sorted by

View all comments

Show parent comment

1

u/Tyulis Feb 21 '21

It’s to pack data in a binary format, like you would do in C with a structure. The doc is rather well explained. Here I could have wrote it as text to add a few GB of disk usage, but I chose to just make it more obscure :)

1

u/shiningmatcha Feb 21 '21

I found the doc too brief. That’s why I asked. I only know Python. The doc says the method “Return a bytes object containing the values v1, v2, ...”. How is a bytes object different from a string?

1

u/Tyulis Feb 21 '21 edited Feb 21 '21

A bytes object in python is just a string of bytes, not encoded like a str is. So struct helps to pack values as raw bytes — for example, on my code above (please don’t learn from it ^^"), it packs integer values into 4 bytes each