Re: Duck Typing and **kwds

2007-10-11 Thread Eduardo O. Padoan
On 10/11/07, Luis Zarrabeitia <[EMAIL PROTECTED]> wrote:
>
> Hi there.
>
> I just tried this test:
>
> 
> def f(**kwds):
> print kwds
>
> import UserDict
> d = UserDict.UserDict(hello="world")
> f(**d)
> 
>
> And it fails with a TypeError exception ("f() argument after ** must be a
> dictionary"). I find that weird, as UserDict should support all protocols that
> dict supports, yet it doesn't seem to support ** unpacking. If instead of
> UserDict I use a derivate class from dict (class mydict(dict):pass), the **
> operator works as expected. It also works if I execute f(**dict(d)) instead.
>
> Is that behavior expected? Is there any reason (performance, perhaps?) to 
> break
> duck-typing in this situation?

Reported, accepted and closed 5 months ago:
http://bugs.python.org/issue1686487

-- 
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Duck Typing and **kwds

2007-10-10 Thread Kay Schluehr
On 11 Okt., 06:05, Luis Zarrabeitia <[EMAIL PROTECTED]> wrote:

> Is that behavior expected? Is there any reason (performance, perhaps?) to 
> break
> duck-typing in this situation?

I guess it wasn't considered to be relevant writing a coercion
function since there aren't too many dict like types that are not
dicts. You can examine what happens in the relevant code fragment in
ceval.c

if (flags & CALL_FLAG_KW) {
kwdict = EXT_POP(*pp_stack);
if (!(kwdict && PyDict_Check(kwdict))) {
PyErr_Format(PyExc_TypeError,
 "%s%s argument after ** "
 "must be a dictionary",
 PyEval_GetFuncName(func),
 PyEval_GetFuncDesc(func));
goto ext_call_fail;
}
}
if (flags & CALL_FLAG_VAR) {
stararg = EXT_POP(*pp_stack);
if (!PyTuple_Check(stararg)) {
PyObject *t = NULL;
t = PySequence_Tuple(stararg);
if (t == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Format(PyExc_TypeError,
 "%s%s argument after * "
 "must be a sequence",
 PyEval_GetFuncName(func),
 PyEval_GetFuncDesc(func));
}
goto ext_call_fail;


I paralleled this with the subsequent clause for tuples where some
coercion function is applied ( PySequence_Tuple ).

It would be cleaner to implement a simple conversion function
PyDictlike_Dict

which seeks for a __dictionary__ method providing the following
interface

__dictionary__(self) -> dict

and is called when being available. Maybe you can suggest this at
python-dev? It might not require a PEP.

-- 
http://mail.python.org/mailman/listinfo/python-list


Duck Typing and **kwds

2007-10-10 Thread Luis Zarrabeitia

Hi there.

I just tried this test:


def f(**kwds):
print kwds

import UserDict
d = UserDict.UserDict(hello="world")
f(**d)


And it fails with a TypeError exception ("f() argument after ** must be a
dictionary"). I find that weird, as UserDict should support all protocols that
dict supports, yet it doesn't seem to support ** unpacking. If instead of
UserDict I use a derivate class from dict (class mydict(dict):pass), the **
operator works as expected. It also works if I execute f(**dict(d)) instead.

Is that behavior expected? Is there any reason (performance, perhaps?) to break
duck-typing in this situation?

Regards,

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie




--
"Al mundo nuevo corresponde la Universidad nueva"
UNIVERSIDAD DE LA HABANA
280 aniversario 
-- 
http://mail.python.org/mailman/listinfo/python-list