interesting bound-built-in-method technique
matt
matt at virtualspectator.com
Sat Jan 20 17:04:18 EST 2001
On Sun, 21 Jan 2001, Quinn Dunkan wrote:
> On Thu, 18 Jan 2001 16:46:01 GMT, Chris Ryland <cpr at emsoftware.com> wrote:
> >Reading the Quixote sources taught me an interesting little "hack"
> >using bound-built-in methods:
> >
> >>>> m = {'foo': 1, 'bar': 0}.has_key
> >>>> m('foo')
> >1
> >>>> m('bar')
> >0
> >
> >Is this a common idiom in Python? Very clever for turning a dictionary
> >lookup into a functional form.
> >
> >Are there other clever but generally obscure idia? (Idioms? ;-)
>
except that your example has an error ..... m('bar') should return 1 since
your bound method should return 'true', not the value of that item.
eg :
>>> m = {'foo':'one','bar':'two'}.has_key
>>> m('foo')
1
>>> m('bar')
1
>>>
More information about the Python-list
mailing list