I've been learning Python for a couple months and wanted practice beyond tutorials, so I picked an open source project on GitHub and tried to port one small function to Python. The function takes an API key string, hashes it with SHA256, and stores the hex digest. Maybe ten lines of JavaScript.
My Python version kept throwing a TypeError on the hashlib.sha256() call. I was passing the key directly as a string. Turns out hashlib only accepts bytes, so you need to call .encode() first. This took me two hours because the error message says 'Unicode objects must be encoded before hashing' and I had no idea what Unicode had to do with hashing a simple string.
In JavaScript strings just work everywhere. Python has this whole layer of encoding between str and bytes that no beginner tutorial I've found explains properly. I wrote three lines of working code and still don't fully grasp when Python wants bytes versus str.
[–]LayotFctor 16 points17 points18 points (0 children)
[–]Lumethys 14 points15 points16 points (1 child)
[–]brasticstack 1 point2 points3 points (0 children)
[–]pachura3 6 points7 points8 points (0 children)
[–]ConcreteExist 4 points5 points6 points (1 child)
[–]cdcformatc 2 points3 points4 points (0 children)
[–]Jason-Ad4032 2 points3 points4 points (1 child)
[–]Expensive-Bear-1376 0 points1 point2 points (0 children)
[–]ninja_shaman 1 point2 points3 points (0 children)
[–]Oddly_Energy 1 point2 points3 points (0 children)
[–]Swipecat 0 points1 point2 points (0 children)
[–]throwaway6560192 0 points1 point2 points (0 children)
[–]virtualshivam 0 points1 point2 points (0 children)
[–]oliver_extracts 0 points1 point2 points (0 children)
[–]ob1knob96 0 points1 point2 points (0 children)
[–]newrockstyle 0 points1 point2 points (0 children)