<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Emma's Blog - silly</title><link>https://emmatyping.dev/</link><description/><atom:link href="https://emmatyping.dev/feeds/silly/rss.xml" rel="self"/><lastBuildDate>Mon, 04 Jun 2018 00:00:00 -0700</lastBuildDate><item><title>Stupid solutions for stupid problems</title><link>https://emmatyping.dev/stupid-solutions-for-stupid-problems.html</link><description>&lt;p&gt;A while back, a friend of mine was working on a coding challenge for some internship. The prompt said to create a dictionary with string keys and string values in your favorite programming language, without using the built in dictionary type.&lt;/p&gt;
&lt;p&gt;This got me thinking of how I would solve this, and this terrible monster is what I came up with:&lt;/p&gt;
&lt;div class="codehilite" style="background: #0d1117"&gt;&lt;pre style="line-height: 125%;"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style="color: #ff7b72"&gt;class&lt;/span&gt; &lt;span style="color: #f0883e; font-weight: bold"&gt;DumbDict&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;:&lt;/span&gt;
    &lt;span style="color: #d2a8ff; font-weight: bold"&gt;__getitem__&lt;/span&gt; &lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #e6edf3"&gt;getattr&lt;/span&gt;
    &lt;span style="color: #d2a8ff; font-weight: bold"&gt;__setitem__&lt;/span&gt; &lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #e6edf3"&gt;setattr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a very silly solution. I don't feel bad about it. Here it is in action:&lt;/p&gt;
&lt;div class="codehilite" style="background: #0d1117"&gt;&lt;pre style="line-height: 125%;"&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style="color: #ff7b72; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;d&lt;/span&gt; &lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #e6edf3"&gt;DumbDict()&lt;/span&gt;
&lt;span style="color: #ff7b72; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;d[&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;#39;hi&amp;#39;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;]&lt;/span&gt; &lt;span style="color: #ff7b72; font-weight: bold"&gt;=&lt;/span&gt; &lt;span style="color: #a5d6ff"&gt;&amp;#39;test&amp;#39;&lt;/span&gt;
&lt;span style="color: #ff7b72; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;d[&lt;/span&gt;&lt;span style="color: #a5d6ff"&gt;&amp;#39;hi&amp;#39;&lt;/span&gt;&lt;span style="color: #e6edf3"&gt;]&lt;/span&gt;
&lt;span style="color: #a5d6ff"&gt;&amp;#39;test&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;How does it work? Well, in Python &lt;code&gt;__getitem__&lt;/code&gt; is the protocol for subscribtion access. When I write &lt;code&gt;d['hi']&lt;/code&gt; Python internally calls &lt;code&gt;d.__getitem__('hi')&lt;/code&gt;. So whats the deal with the &lt;code&gt;getattr&lt;/code&gt; call then?&lt;/p&gt;
&lt;p&gt;In Python, everything is an object. By default, objects internally map attribute names to values. In my dictionary implementation, I take advantage of this to use Python's internals to create my dictionary.&lt;/p&gt;
&lt;p&gt;The pros of this dictionary are that it is fast. It is close (within 10%) of the builtin dictionary type (there is a bit of overhead with function calls).&lt;/p&gt;
&lt;p&gt;There are quite a few cons, the first of which several of the more knowledable among you have already realized. I'm totally cheating here. Well, maybe. Or I'm not. Technically, classes use something called &lt;code&gt;types.MappingProxyType&lt;/code&gt; to manage attribute mappings. This is however an implementation detail, so its up to personal opinion whether I'm using a built in dictionary or not. Anyway, I think its pretty cool. And remember I did say it was stupid...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Emma Smith</dc:creator><pubDate>Mon, 04 Jun 2018 00:00:00 -0700</pubDate><guid>tag:emmatyping.dev,2018-06-04:/stupid-solutions-for-stupid-problems.html</guid><category>misc</category><category>silly</category><category>python</category></item></channel></rss>