Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > duck typing at will

Reply
Thread Tools

duck typing at will

 
 
Jose Mora
Guest
Posts: n/a
 
      12-30-2008
Duck typing is called that way because "If it looks like a duck and
quacks like a duck, it must be a duck." I think it would be good to
have also "If the programmer wants to deal with it like a duck, it
must be a duck"

I mean, some tasks are rather boring in python when compared with php,
for example, let's imagine we have a dictionary that contains
dictionaries that contain the times that a key appears. We, or at
least I, would like to write something as short as:

dict[k1][k2] += 1

However we will have to do a longer code (this is the smallest code I
could come up with):

dict = {}
if not k1 in dict:
dict[k1] = {}
if not k2 in dict[k1]:
dict[k1][k2] = 0
dict[k1][k2] += 1

I know it is not the Apocalypse nor something really important when
compared with other things, but maybe it would be better if it wasn't
necessary to declare the variables when they are built-in types or to
initialize the keys in a dictionary, having special values (the
identity element of the operation that causes the initialization) to
initialize when it has not been done, initializing with the most
general type that supports the operation that causes the
initialization, casting to other type if necessary after that.

This is just an idea, maybe I'm not the first one suggesting it, or
maybe it is completely impossible to implement it because it would
require deep changes in python, but I wanted to discuss it.

Best regards.
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-30-2008
Jose Mora a écrit :
> Duck typing is called that way because "If it looks like a duck and
> quacks like a duck, it must be a duck."


or at least something close enough...

> I think it would be good to
> have also "If the programmer wants to deal with it like a duck, it
> must be a duck"


DWIM[1] just doesn't work. No parser can read your mind, and the harder
they try, the more bugs they are likely to cause. IOW : cows neither
look nor quack like ducks, and asking a cow to quack just won't turn
your cow into a duck.


[1] Do What I Mean

> I mean, some tasks are rather boring in python when compared with php,


Possibly. OTHO, and from years of experience with both languages, a lot
of tasks are way more boring in php when compared to Python.

> for example, let's imagine we have a dictionary that contains
> dictionaries that contain the times that a key appears. We, or at
> least I, would like to write something as short as:
>
> dict[k1][k2] += 1
>
> However we will have to do a longer code (this is the smallest code I
> could come up with):


> dict = {}


bad identifier - it shadows the builtin dict type.

> if not k1 in dict:
> dict[k1] = {}
> if not k2 in dict[k1]:
> dict[k1][k2] = 0
> dict[k1][k2] += 1


What about:

from collections import defaultdict
d = defaultdict(lambda: defaultdict(int))

for k1, k2 in whatever:
d[k1][k2] += 1



NB : this requires a "recent" Python version (works on 2.5.1 at least).
Else, yes, this will be a bit more "boring":

d = dict()
for k1, k2 in whatever:
d2 = d.setdefault(k1, dict())
d2[k2] = d2.get(k2, 0) + 1


HTH

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
duck duck go peter NZ Computing 1 01-09-2011 05:19 AM
Wanted: Citation supporting Duck Typing Paddy Python 1 05-09-2008 06:37 PM
explicit protocols and duck typing dg.google.groups@thesamovar.net Python 2 01-28-2008 12:26 AM
Duck typing alows true polymorfisim atbusbook@aol.com Python 15 09-01-2006 03:56 PM
Duck Typing srijit@yahoo.com Python 10 09-17-2003 03:48 AM



Advertisments