From: Steven D'Aprano on 8 Jun 2010 12:57 On Tue, 08 Jun 2010 08:36:00 -0600, Ian Kelly wrote: >> 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. So they are. I've just learned something new, thanks. -- Steven
From: Terry Reedy on 8 Jun 2010 15:12 On 6/8/2010 2:18 AM, Terry Reedy wrote: > On 6/7/2010 6:03 PM, Peter Otten wrote: > >> The following experiment shows that you only need to implement a >> keys() and >> __getitem__() method. >> >> $ cat kw.py >> class A(object): >> def keys(self): return list("ab") >> def __getitem__(self, key): >> return 42 >> >> def f(**kw): >> print(kw) >> >> f(**A()) >> $ python kw.py >> {'a': 42, 'b': 42} >> >> However, if you have A inherit from dict... >> >> $ cat kwd.py >> class A(dict): >> def keys(self): return list("ab") >> def __getitem__(self, key): >> return 42 >> >> def f(**kw): >> print(kw) >> >> f(**A()) >> $ python kwd.py >> {} >> >> 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. > > In 2.6, the requirement changed from '(subclass of) dictionary' to > 'mapping' so this is a bit strange. It sort of looks like a bug. I will > test with 3.1 tomorrow (later today, actually). Verified and report filed at http://bugs.python.org/issue8945 Terry Jan Reedy
First
|
Prev
|
Pages: 1 2 3 4 Prev: Python + vim + spaces vs tab Next: PyCon Australia 2010 registration deadline reminder |