I realized that python already has a way to access the string-based members
of a dict without using quotes:
def expect_a_chair(chair, **kw):
print "Thanks. That chair is %s." % chair
if kw:
for key, val in kw.iteritems():
print "I wasn't expecting the (%s) %s!" % (val, key)
d = json
>
>
> You're correct, this is trivial with object_hook.
>
> >>> class AttrDict(dict):
> ... def __getattr__(self, attr):
> ... try:
> ... return self[attr]
> ... except KeyError:
> ... raise AttributeError(attr)
> ...
> >>> import json
> >>> obj = json.lo
>
>
> If you need this for **kw arguments maybe you're not using them right;
> why not name your arguments if you're going to reference them by name?
>
Good point.
>
> The JSON use case seems to be driven because this is the way
> JavaScript does things -- they don't distinguish between dicts and
a
long name ("{'spam':'eggs'}.as_attributes.spam == 'eggs' "). That latter
thing could, in theory, break working code... but only if you're using the
same attribute name to duck-type a dictionary subclass, which I'd bet heavy
odds has not been done for th
;argument"? Why shouldn't I be able to say:
def fun2(**kw):
print kw..argument
(in real life, there would be a try... except block in case there was no
argument, I'm just showing the simplest case here.)
Jameson
2011/3/24 Jameson Quinn
>
>
> 2011/3/24 Brian Curt
2011/3/24 Brian Curtin
> On Thu, Mar 24, 2011 at 06:40, Jameson Quinn wrote:
>
>> "class attrdict" is a perennial dead-end for intermediate pythonistas who
>> want to save 3 characters/5 keystrokes for item access. Other languages such
>> as javascript allow
"class attrdict" is a perennial dead-end for intermediate pythonistas who
want to save 3 characters/5 keystrokes for item access. Other languages such
as javascript allow "somedict.foo" to mean the same as "somedict['foo']", so
why not python? Well, there are a number of reasons why not, beginning