Skip to content

Commit

Permalink
Deployed 22f2079 with MkDocs version: 1.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
drdv committed Dec 9, 2024
1 parent c75c712 commit ecc394e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions blog/202412-python-strings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,15 @@ <h3 id="bytes-objects">Bytes objects<a class="headerlink" href="#bytes-objects"
compatibility with ASCII and efficient data storage, while UTF-16 and UTF-32 allow for
faster processing of a larger range of characters. Having the possibility to
easily/efficiently change representations is convenient.</p>
<p>Of course, bytes objects can be used in other contexts as well. For example, <code class="language-python highlight"><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">to_bytes</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">byteorder</span><span class="o">=</span><span class="s1">&#39;little&#39;</span><span class="p">)</span></code> would return the bytes representation of the
integer <code>1</code> (in little endian).</p>
<p>Bytes do not necessarily have to be associated with individual code points, as is the
case when using <code class="language-python highlight"><span class="nb">str</span><span class="o">.</span><span class="n">encode</span></code>. For example, suppose we want to express the
string <code class="language-python highlight"><span class="s2">&quot;a1b1&quot;</span></code> as a byte object, where each pair of characters represents a
byte in hex (i.e., <code>0xA1</code> followed by <code>0xB1</code>). In this case, using <code class="language-python highlight"><span class="nb">list</span><span class="p">(</span><span class="s2">&quot;a1b1&quot;</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span></code> is not appropriate, as it would return <code>[97, 49, 98, 49]</code>, which
are the ASCII codes for the characters <code>a</code>, <code>1</code>, <code>b</code>, and <code>1</code>, respectively. Instead, we
should consider the additional structure and use <code class="language-python highlight"><span class="nb">list</span><span class="p">(</span><span class="nb">bytes</span><span class="o">.</span><span class="n">fromhex</span><span class="p">(</span><span class="s2">&quot;a1b1&quot;</span><span class="p">))</span></code>,
which results in <code>[161, 177]</code>.</p>
<p>Bytes objects can also be used in other contexts. For instance, <code class="language-python highlight"><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">to_bytes</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">byteorder</span><span class="o">=</span><span class="s1">&#39;little&#39;</span><span class="p">)</span></code> returns the byte representation of the integer 1
(in little-endian).</p>
<h2 id="immutability">Immutability<a class="headerlink" href="#immutability" title="Permanent link">#</a></h2>
<p>The design decision to have immutable string in python has far-reaching implication
related to e.g., hashing, performance optimizations, garbage collection, thread safety
Expand Down
Loading

0 comments on commit ecc394e

Please sign in to comment.