Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] setdefault's second argument

Tim Peterstim.peters at gmail.com
Tue Aug 30 20:55:45 CEST 2005


[Wolfgang Lipp]> reminds me of dict.get()... i think in both cases being explicit::>>     beast = d.setdefault( 666, None )>     beast = d.get( 666, None )>> just reads better, allthemore since at least in my code what comes> next is invariably a test 'if beast is None:...'. so>>     beast = d.setdefault( 666 )>     if beast is None:>         ...Do you actually do this with setdefault()?  It's not at all the sameas the get() example next, because d.setdefault(666) may _also_ havethe side effect of permanently adding a 666->None mapping to d. d.get(...) never mutates d.> and>>     beast = d.get( 666 )>     if beast is None:>         ...>> a shorter but a tad too implicit for my feeling.Nevertheless, 1-argument get() is used a lot.  Outside the test suite,I've only found one use of 1-argument setdefault() so far, and it wasa poor use (used two lines of code to emulate what dict.pop() doesdirectly).


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp