A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20110325/a2a8cbb2/attachment.html below:

I realized that python already has a way to access the string-based members of a dict without using quotes:<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">def expect_a_chair(chair, **kw):</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  print &quot;Thanks. That chair is %s.&quot; % chair</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  if kw:</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    for key, val in kw.iteritems():</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">      print &quot;I wasn&#39;t expecting the (%s) %s!&quot; % (val, key)</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">d = json.loads(&#39;{&quot;chair&quot;:&quot;comfy&quot;,&quot;inquisition&quot;:&quot;Spanish&quot;}&#39;)</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">expect_a_chair(**d)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">try:</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  expect_a_chair({})</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">except TypeError:</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  print &quot;No chair.&quot;</font></div>

<div><br></div><div>The ** operator does this. Notice that nowhere in that python code (not counting the json) do I have to put &quot;chair&quot; in quotes.</div><div><br></div><div>Honestly, this solves my current use case. I can use functions like expect_a_chair for everything I need right now.</div>

<div><br></div><div>But perhaps, if there were a quote-free way to access string-based dict items, it should be based on this. The problem is, this ** operator is a unary operator, and the non-unary ** is already taken. </div>

<div><br></div><div>So, I don&#39;t have a perfect name for my proposed quoteless synonym for &#39;[&quot;...&quot;]&#39;. My best option is &#39;*.&#39;. Note that this could also be used for unpacking, and with defaults:</div>

<div>d*.(x,y,z) #=== (d[&quot;x&quot;], d[&quot;y&quot;], d[&#39;z&#39;])</div><div>e=d*.(e=None) #like &#39;e=d.get(&quot;e&quot;, None)&#39;. </div><div><br></div><div>Does this sound worth-it to anyone?</div><div><br>


</div>
<div>Jameson</div>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4