title: Comparison of byte literals in Python
body: <p>The following question arose because I was trying to use <code>bytes</code> strings as dictionary keys and bytes values that I understood to be equal weren't being treated as equal.</p>
  <p>Why doesn't the following Python code compare equal - aren't these two equivalent representations of the same binary data (the example is knowingly chosen to avoid endianness)?</p>
  <pre><code>b'0b11111111' == b'0xff'
  </code></pre>
  <p>I know the following evaluates true, demonstrating the equivalence:</p>
  <pre><code>int(b'0b11111111', 2) == int(b'0xff', 16)
  </code></pre>
  <p>But why does Python force me to know the representation? Is it related to endianness? Is there some easy way to force these to compare equivalent other than converting them all to, e.g., hexadecimal literals? Is there a transparent and clear method to move between all representations in a (somewhat) platform independent way (or am I asking too much)?</p>
  <p>Say I want to actually index a dictionary using 8 bits in the form <code>b'0b11111111'</code>, then why does Python expand it to ten bytes and how do I prevent that?</p>
  <p>This is a smaller piece of a large tree data structure and expanding my indexing by a factor of 80 seems like a huge waste of memory.</p>