Re: Common Python Idioms

2006-12-15 Thread Kent Johnson
Fredrik Lundh wrote: > Stephen Eilert wrote: > >> I do think that, if it is faster, Python should translate >> "x.has_key(y)" to "y in x". > > http://svn.python.org/view/sandbox/trunk/2to3/fix_has_key.py?view=markup Seems to have moved to here: http://svn.python.org/view/sandbox/trunk/2to3/fixes

Re: Common Python Idioms

2006-12-08 Thread Steven D'Aprano
On Fri, 08 Dec 2006 17:42:56 +0100, Daniel Dittmar wrote: > Marc 'BlackJack' Rintsch wrote: >> If ``in`` shouldn't work with dictionaries, either `__contains__()` must >> be implemented to throw an exception or dictionaries shouldn't be iterable. > > I agree completely (in the sense that dictiona

Re: Common Python Idioms

2006-12-08 Thread Stephen Eilert
Ben Finney escreveu: > "Stephen Eilert" <[EMAIL PROTECTED]> writes: > > > Is there a list somewhere listing those not-so-obvious-idioms? > > They're only not-so-obvious to those who learn one version of Python > and then ignore release notes on future versions. The "k in some_dict" > was a wonder

Re: Common Python Idioms

2006-12-08 Thread Fredrik Lundh
Daniel Dittmar wrote: > I agree completely (in the sense that dictionaries shouldn't be iterable > directly). Probably even more strongly, at least every time I see some > code where someone iterates over the keys, only to use the key to look > up the value (instead if using iteritms). so? th

Re: Common Python Idioms

2006-12-08 Thread Daniel Dittmar
Marc 'BlackJack' Rintsch wrote: > If ``in`` shouldn't work with dictionaries, either `__contains__()` must > be implemented to throw an exception or dictionaries shouldn't be iterable. I agree completely (in the sense that dictionaries shouldn't be iterable directly). Probably even more strongly,

Re: Common Python Idioms

2006-12-08 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Daniel Dittmar wrote: > John Machin wrote: >> No it doesn't look wrong to anyone who has read the docs on >> sys.modules. > > My point was really that there is no obvious implementation for 'in' on > dictionaries, so it should have been left out. And that GvR thought so

Re: Common Python Idioms

2006-12-08 Thread Daniel Dittmar
John Machin wrote: > No it doesn't look wrong to anyone who has read the docs on > sys.modules. My point was really that there is no obvious implementation for 'in' on dictionaries, so it should have been left out. And that GvR thought so for quite some time as well (until he got mixed up with

Re: Common Python Idioms

2006-12-08 Thread George Sakkis
Steven D'Aprano wrote: > On Thu, 07 Dec 2006 13:52:33 -0800, George Sakkis wrote: > > > I'm surprized that none of these pages mentions the incompatible type > > comparison gotcha: > > > 5 < "4" > > True > > > > I'm sure this has bitten many folks, particularly (ex) Perl'ers. > > Why is this

Re: Common Python Idioms

2006-12-07 Thread Gabriel Genellina
At Thursday 7/12/2006 19:45, Steven D'Aprano wrote: > I'm surprized that none of these pages mentions the incompatible type > comparison gotcha: > 5 < "4" > True > > I'm sure this has bitten many folks, particularly (ex) Perl'ers. Why is this a gotcha? I can't speak for others, but except

Re: Common Python Idioms

2006-12-07 Thread Ben Finney
"Stephen Eilert" <[EMAIL PROTECTED]> writes: > Is there a list somewhere listing those not-so-obvious-idioms? They're only not-so-obvious to those who learn one version of Python and then ignore release notes on future versions. The "k in some_dict" was a wonderful thing to read in the release no

Re: Common Python Idioms

2006-12-07 Thread Steven D'Aprano
On Thu, 07 Dec 2006 13:52:33 -0800, George Sakkis wrote: > I'm surprized that none of these pages mentions the incompatible type > comparison gotcha: > 5 < "4" > True > > I'm sure this has bitten many folks, particularly (ex) Perl'ers. Why is this a gotcha? I can't speak for others, but

Re: Common Python Idioms

2006-12-07 Thread George Sakkis
Danny Colligan wrote: > > Is there a list somewhere listing those not-so-obvious-idioms? > > I don't know about lists of not-so-obvious idioms, but here's some > gotchas (there may be some overlap with what you're asking about): > > http://zephyrfalcon.org/labs/python_pitfalls.html > http://www.fer

Re: Common Python Idioms

2006-12-07 Thread John Machin
Daniel Dittmar wrote: > Duncan Booth wrote: > > In this case, dict objects used to not support the 'in' operator, but at > > some point it was added. I believe it wasn't there originally because Guido > > wasn't sure whether people would expect it should match keys or keys and > > values. > > And

Re: Common Python Idioms

2006-12-07 Thread Danny Colligan
> Is there a list somewhere listing those not-so-obvious-idioms? I don't know about lists of not-so-obvious idioms, but here's some gotchas (there may be some overlap with what you're asking about): http://zephyrfalcon.org/labs/python_pitfalls.html http://www.ferg.org/projects/python_gotchas.html

Re: Common Python Idioms

2006-12-07 Thread Fredrik Lundh
Stephen Eilert wrote: > I do think that, if it is faster, Python should translate > "x.has_key(y)" to "y in x". http://svn.python.org/view/sandbox/trunk/2to3/fix_has_key.py?view=markup -- http://mail.python.org/mailman/listinfo/python-list

Re: Common Python Idioms

2006-12-07 Thread Stephen Eilert
Duncan Booth escreveu: > > > > > Is there a list somewhere listing those not-so-obvious-idioms? I've > > seen some in this thread (like the replacement for .startswith). > > > The release notes for each new version. Unfortunately the rest of the > documentation sometimes lags behind the release n

Re: Common Python Idioms

2006-12-07 Thread Daniel Dittmar
Duncan Booth wrote: > In this case, dict objects used to not support the 'in' operator, but at > some point it was added. I believe it wasn't there originally because Guido > wasn't sure whether people would expect it should match keys or keys and > values. And he was right: import sys 'sys' i

Re: Common Python Idioms

2006-12-07 Thread Duncan Booth
"Stephen Eilert" <[EMAIL PROTECTED]> wrote: > I've always used has_key(), thinking it was the only way to do it. > Given that Python says that "There Should Be Only One Way to Do It", I > didn't bother searching for alternatives. The full quote is actually: There should be one-- and pref

Re: Common Python Idioms

2006-12-07 Thread Steven D'Aprano
On Thu, 07 Dec 2006 04:08:18 -0800, Stephen Eilert wrote: > Given that Python says that "There Should Be Only One Way to Do It", Python says no such thing. Perhaps you should run "import this" at the prompt, and read _carefully_ and take note of the difference between the Zen of Python and what

Re: Common Python Idioms

2006-12-07 Thread Georg Brandl
Stephen Eilert wrote: > Hendrik van Rooyen escreveu: > >> <[EMAIL PROTECTED]> wrote: >> >> > Peter> Bjoern Schliessmann wrote: >> > >> Wouldn't be "if k in d.keys()" be the exact replacement? >> > >> > Peter> No, 'k in d' is equivalent to 'd.has_key(k)', only with less >> > Peter>

Common Python Idioms

2006-12-07 Thread Stephen Eilert
Hendrik van Rooyen escreveu: > <[EMAIL PROTECTED]> wrote: > > > Peter> Bjoern Schliessmann wrote: > > >> Wouldn't be "if k in d.keys()" be the exact replacement? > > > > Peter> No, 'k in d' is equivalent to 'd.has_key(k)', only with less > > Peter> (constant) overhead for the func