From: kkumer on
(sorry for posting empty post by accident)

Peter Otten <__peter__(a)web.de> wrote:
> it stops working -- probably a side-effect of some optimization.
> So if you change your hubDict's base class from dict to object you should
> get the desired behaviour.

Yes, as already noted, this would require python >=1.6, and I am
reluctant to upgrade at this point.

What I ended up doing is to simply create a normal dictionary
and copy all items from my hubDict merged "dictionary" into it
just for the purpose of this one function call

auxdict = dict((it for it in hubDictinstance.items()))
f(**auxdict)

Since this happens only few times during runtime it's not a big deal.

Thanks to all for insights.

K.
From: kkumer on
Bryan <bryanjugglercryptographer(a)yahoo.com> wrote:
> I get the same bug-like behavior in 3.1. I think Peter is right that
> it's probably a side-effect of an optimization. kkumer seems to have
> completely over-ridden the methods of dict, but if we insert into his
> hubDict with the parent class's method:
>
> dict.__setitem__(dh, 'c', 3)
>
> Then the **dh argument passes the keyword arg c=3.

Yes. But problem is that this setting of item
should also affect one of the two original parent dicts,
while your proposal affects only hubDict instance.
(hubDict is never used to create new items, just to change
values of existing ones, which belong to one of
two parents).

K.
From: Steven D'Aprano on
On Mon, 07 Jun 2010 15:04:35 -0600, Ian Kelly wrote:

> I don't think that what you want to do here is possible. It appears to
> be hard-coded and not affected by subclassing, as evidenced by the
> following snippet:
>
> class NonDict(dict):
> for attr in dict.__dict__:
> if attr not in ('__init__', '__new__',):
> locals()[attr] = None


I'm afraid your test is invalid. As the documentation states, you CANNOT
write to locals() -- the change doesn't stick. This is nothing to do with
dicts.

>>> def f():
.... x = 1
.... locals()['x'] = 2
.... print x
....
>>> f()
1




--
Steven
From: Bryan on
kkumer wrote:
> Bryan wrote:
> > I get the same bug-like behavior in 3.1. I think Peter is right that
> > it's probably a side-effect of an optimization. kkumer seems to have
> > completely over-ridden the methods of dict, but if we insert into his
> > hubDict with the parent class's method:
>
> >  dict.__setitem__(dh, 'c', 3)
>
> > Then the **dh argument passes the keyword arg c=3.
>
> Yes. But problem is that this setting of item
> should also affect one of the two original parent dicts,
> while  your proposal affects only hubDict instance.
> (hubDict is never used to create new items, just to change
> values of existing ones, which belong to one of
> two parents).

Right. This is helpful group, but my observation here does not solve
your immediate problem. Your work-around seems reasonable, and Peter
Otten's demo of what happens if you inherit from object rather than
dict might make **dh work as you want.


--
--Bryan
From: Ian Kelly on
On Tue, Jun 8, 2010 at 4:21 AM, Steven D'Aprano
<steve(a)remove-this-cybersource.com.au> wrote:
> On Mon, 07 Jun 2010 15:04:35 -0600, Ian Kelly wrote:
>
>> I don't think that what you want to do here is possible.  It appears to
>> be hard-coded and not affected by subclassing, as evidenced by the
>> following snippet:
>>
>> class NonDict(dict):
>>     for attr in dict.__dict__:
>>         if attr not in ('__init__', '__new__',):
>>             locals()[attr] = None
>
>
> I'm afraid your test is invalid. As the documentation states, you CANNOT
> write to locals() -- the change doesn't stick. This is nothing to do with
> dicts.

Huh, good point. But actually the docs just say that the changes
aren't guaranteed to stick, and in the case of my test I inspected the
class and the methods were indeed replaced with None. In fact, when I
first wrote it, I didn't include the if statement and got a TypeError
when I tried to construct an instance.

Cheers,
Ian