<br><br><div class="gmail_quote">2011/3/25 Josiah Carlson <span dir="ltr"><<a href="mailto:josiah.carlson@gmail.com">josiah.carlson@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div><div></div><div class="h5">On Fri, Mar 25, 2011 at 9:00 AM, Jameson Quinn <<a href="mailto:jameson.quinn@gmail.com">jameson.quinn@gmail.com</a>> wrote:<br>
> I realized that python already has a way to access the string-based members<br>
> of a dict without using quotes:<br>
> def expect_a_chair(chair, **kw):<br>
>   print "Thanks. That chair is %s." % chair<br>
>   if kw:<br>
>     for key, val in kw.iteritems():<br>
>       print "I wasn't expecting the (%s) %s!" % (val, key)<br>
> d = json.loads('{"chair":"comfy","inquisition":"Spanish"}')<br>
> expect_a_chair(**d)<br>
> try:<br>
>   expect_a_chair({})<br>
> except TypeError:<br>
>   print "No chair."<br>
> The ** operator does this. Notice that nowhere in that python code (not<br>
> counting the json) do I have to put "chair" in quotes.<br>
> Honestly, this solves my current use case. I can use functions like<br>
> expect_a_chair for everything I need right now.<br>
> But perhaps, if there were a quote-free way to access string-based dict<br>
> items, it should be based on this. The problem is, this ** operator is a<br>
> unary operator, and the non-unary ** is already taken.<br>
> So, I don't have a perfect name for my proposed quoteless synonym for<br>
> '["..."]'. My best option is '*.'. Note that this could also be used for<br>
> unpacking, and with defaults:<br>
> d*.(x,y,z) #=== (d["x"], d["y"], d['z'])<br>
> e=d*.(e=None) #like 'e=d.get("e", None)'.<br>
> Does this sound worth-it to anyone?<br>
<br>
</div></div>Gut reaction:<br>
Dear gods, how and why does * have a . operator, and why is there a<br>
function being called on it?!?<br>
Huh; x,y,z aren't defined in that scope, that's gotta return a<br>
NameError for sure.<br>
Or wait, is that some new way to specify a vector for multiplication?<br>
When did Python get a vector type and literal?<br>
<br>
If punctuation is the answer, Perl is the question. Since this is<br>
Python, and punctuation is decidedly not the answer, I'm going to go a<br>
little further than before: -sys.maxint<br>
<br>
Regards,<br>
<font color="#888888"> - Josiah<br>
</font></blockquote></div><br><div>OK. As I said, I think that the existing functionality as I showed in expect_a_chair solves my dislike for using quotes for something which logically is an attribute name. (It would be even better if json had a way to distinguish object-like data, with well-defined parameters, from more free-form data, but that is out of scope here.)</div>

<div><br></div><div>Jameson</div><div><br></div><div>ps. A true masochist could do something like my unpacking '*.' by a horrendous abuse of the lambda syntax:</div><div>(lambda x,y,z,e=None, **kw:oh_please_god_no(locals()))(**d)</div>

<div>I leave the definition of oh_please_god_no as an exercise for the reader. :)</div>