Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > <dict>.setdefault()

Reply
Thread Tools

<dict>.setdefault()

 
 
Tino Lange
Guest
Posts: n/a
 
      07-31-2003
Hi!

I just realized that <dict>.setdefault *always* executes the second
argument - even if it's not necessary, because the requested item in
the first argument exists.

This is not what I expected - why evaluate the second argument if it's
not needed? Also this can lead to side-effects!

Example:

>>> a = {}
>>> b = {}
>>> a.setdefault(1,b.setdefault(1,1))

1

'1' didn't exist in a - so I expect 'b.setdefault(1,1)' to be
evaluated. Great.

>>> a.setdefault(1,b.setdefault(2,1))

1

'1' existed, so it's not necessary to evaluate 'b.setdefault(2,1)'.
But:

>>> b

{2: 1, 1: 1}

.... shows that it was executed!

So it's not equivalent to:

if 1not in a:
b.setdefault(2,1)

Is this really by design? If there's a complicated, expensive to
calculate/build 2nd argument (maybe a function call) then it's also
quite ineffective to evaluate it just to throw away...

Thanks!

Tino

 
Reply With Quote
 
 
 
 
Alexandre Fayolle
Guest
Posts: n/a
 
      07-31-2003
Tino Lange a écrit :
> I just realized that <dict>.setdefault *always* executes the second
> argument - even if it's not necessary, because the requested item in
> the first argument exists.


Since setdefault is a method, this is coherent with Python's standard
behaviour: method and function arguments are always evaluated before the
method is called.

--
Alexandre Fayolle
LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
Développement logiciel avancé - Intelligence Artificielle - Formations
 
Reply With Quote
 
 
 
 
Tino Lange
Guest
Posts: n/a
 
      07-31-2003
On Thu, 31 Jul 2003 16:54:46 +0000 (UTC), Alexandre Fayolle
<> wrote:

>> I just realized that <dict>.setdefault *always* executes the second
>> argument - even if it's not necessary, because the requested item in
>> the first argument exists.


> method and function arguments are always evaluated before the
>method is called.


Yep. You're right. Of course it's like that.
Anyway - that's not what I expected in the first thought.

It shows that less code is not always the best code...

Thanks!

Tino
 
Reply With Quote
 
Tino Lange
Guest
Posts: n/a
 
      07-31-2003
On 31 Jul 2003 18:01:51 +0100, (John J. Lee) wrote:

>Because that's the way Python always does function calls?


Of course. From the consistency point of view... perfectly right!
(I thought that maybe because these are very special low-level methods
for built-in types, it might be different....)

>Don't, then. Use key in dict, or dict.has_key(key).


or the C-like style:
>>> a[3] = a.get(1) or b.setdefault(4,1)


Thanks!

Tino

 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      07-31-2003
Tino Lange wrote:

> I just realized that <dict>.setdefault *always* executes the second
> argument - even if it's not necessary, because the requested item in
> the first argument exists.
>
> This is not what I expected - why evaluate the second argument if it's
> not needed? Also this can lead to side-effects!


Because Python doesn't have macros or special forms that look like
functions. If you see something that looks like a function, it
evaluates all arguments before it calls the function. Always. Changing
this would cause confusion, not resolve it.

--
Erik Max Francis && && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ There's a reason why we / Keep chasing morning
\__/ Sandra St. Victor
 
Reply With Quote
 
Steven Taschuk
Guest
Posts: n/a
 
      08-01-2003
Quoth Tino Lange:
> On 31 Jul 2003 18:01:51 +0100, (John J. Lee) wrote:

[...]
> >Don't, then. Use key in dict, or dict.has_key(key).

>
> or the C-like style:
> >>> a[3] = a.get(1) or b.setdefault(4,1)


Be careful doing that -- it relies on all possible values for a[1]
being true. Consider:

>>> a = {1: 0}
>>> b = {}
>>> a[3] = a.get(1) or b.setdefault(4, 1)
>>> a

{1: 0, 3: 1}
>>> b

{4: 1}

--
Steven Taschuk o- @
7O )
" (

 
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




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57