Who ordered that? #10087
Replies: 3 comments
-
Looks like a minor bug: that comparison is not allowed in Python (tested with v3.5 and v3.8): >>> 'M' in mpy[:1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str' |
Beta Was this translation helpful? Give feedback.
-
Just for the record: In response to the title, who cares about that, especially in this case ? If the file signature is |
Beta Was this translation helpful? Give feedback.
-
@kjm1102 Just to clarify something here that might help -- when you write What @karfas said is correct though -- if you want to compare byte values then you should access individual bytes and compare them by their actual values. i.e. There are a few places in MicroPython where, for code size minimisation, we allow strings and bytes to be interchangeable. i.e. CPython doesn't allow you to mix bytes and strings with the The main thing to remember is in Python, there is no "character" type. This is a deliberate decision which is a lot more sensible when encoding is involved. So when you index a string you get a string, and when you index a bytes/bytearray/memoryview you get an int. When you slice either, you get the original type. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I often think of Isidor Rabi's quip "Who ordered that?" in reference to the unexpected appearance of the muon in the 1930s. It's how I feel when dealing with python types some days, actually most days.
Needed to check the first byte of a file to validate that it was a .mpy file. mpy files start with an M (eg b'M\x05.....). Since I've given up trying to second guess python I sought detail
So easy enough now to check
Then I wondered if it was possible to check by reference to the 'M' representation? Turns out it is
I have no idea why it's necessary to reference the 1st 2 bytes to see if the 1st byte is correct using the 'M' alias but I'd be keen to learn if someone would care to explain it?
Beta Was this translation helpful? Give feedback.
All reactions