473,408 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Duck Typing and **kwds


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
Oct 11 '07 #1
1 2999
On 11 Okt., 06:05, Luis Zarrabeitia <ky...@uh.cuwrote:
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.

Oct 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paradox | last post by:
Why does the following attempts to pass in keywords arguments not work. It would be alot cooler if there was a way to not have to have the function defined with the variable name. It really seems...
10
by: srijit | last post by:
Hello All, I have been seeing this term "duck typing" for quite sometime now. It will be nice if one of us can give an example in Python demonstrating duck typing and/or link to some Python...
15
by: atbusbook | last post by:
lets say you want a generic numerical algorithom like sum Ruby def sum lst lst.inject(0){|total,current| total*current} end Java // i dont know if there is a numeric super class for numbers
0
by: Paddy | last post by:
The official glossary entry here: http://docs.python.org/tut/node18.html#l2h-46 says: " duck-typing Pythonic programming style that determines an object's type by inspection of its method or...
16
by: Joe Strout | last post by:
Let me preface this by saying that I think I "get" the concept of duck- typing. However, I still want to sprinkle my code with assertions that, for example, my parameters are what they're...
0
by: Tim Rowe | last post by:
2008/11/12 Joe Strout <joe@strout.net>: What do you actually mean by "Quacks like a string"? Supports the 'count()' method? Then you find out if it doesn't when you try to apply the 'count()'...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.