Re: False and 0 in the same dictionary

2008-11-06 Thread Paul Rubin
Prateek [EMAIL PROTECTED] writes: How about using (x, type(x)) as the key instead of just x? Yup. I thought of that. Although it seems kinda unpythonic to do so. Especially since the dictionary is basically a cache mostly containing strings. Adding all the memory overhead for the extra tuples

Re: False and 0 in the same dictionary

2008-11-06 Thread John Machin
On Nov 7, 11:54 am, Paul Rubin http://[EMAIL PROTECTED] wrote: Prateek [EMAIL PROTECTED] writes: How about using (x, type(x)) as the key instead of just x? Yup. I thought of that. Although it seems kinda unpythonic to do so. Especially since the dictionary is basically a cache mostly

Re: False and 0 in the same dictionary

2008-11-06 Thread Paul Rubin
John Machin [EMAIL PROTECTED] writes: You could use a second dict for the other type:   def lookup(x):     if x in dict1: return dict1[x]     return dict2[x] dict1 would have the 4 special keys and dict2 would have the regular keys. Ummm how do you get the 4 special keys in

Re: False and 0 in the same dictionary

2008-11-06 Thread John Machin
On Nov 7, 3:05 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: John Machin [EMAIL PROTECTED] writes: You could use a second dict for the other type: def lookup(x): if x in dict1: return dict1[x] return dict2[x] dict1 would have the 4 special keys and dict2 would have the regular

Re: False and 0 in the same dictionary

2008-11-05 Thread bearophileHUGS
Ben Finney: Is there a later PEP that I've missed which finally makes ‘bool’ a type independent from ‘int’? In a tidy language like an ObjectPascal or Java bools and integers are different types. In Python if bools become distinct from integers you have to rewrite things like: sum(el == val

Re: False and 0 in the same dictionary

2008-11-05 Thread bearophileHUGS
Prateek: How do I make a dictionary which has distinct key-value pairs for 0, False, 1 and True. Why do you have to do that? What's the problem you have to solve? Maybe (probably) there are better or more clean alternative solutions. Bye, bearophile --

Re: False and 0 in the same dictionary

2008-11-05 Thread Carl Banks
On Nov 4, 3:48 pm, Prateek [EMAIL PROTECTED] wrote: I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct key-value pairs for 0, False, 1 and True. As I

Re: False and 0 in the same dictionary

2008-11-05 Thread Marc 'BlackJack' Rintsch
On Wed, 05 Nov 2008 04:22:32 -0800, bearophileHUGS wrote: In Python if bools become distinct from integers you have to rewrite things like: sum(el == val for el in iterable) as: sum(1 for el in iterable if el == val) I would expect that you can still cast `bool`\s to `int`\s then.

False and 0 in the same dictionary

2008-11-04 Thread Prateek
I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct key-value pairs for 0, False, 1 and True. As I have learnt, 0 and False both hash to the same value (same

Re: False and 0 in the same dictionary

2008-11-04 Thread Duncan Booth
Prateek [EMAIL PROTECTED] wrote: I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct key-value pairs for 0, False, 1 and True. How about using (x,

Re: False and 0 in the same dictionary

2008-11-04 Thread Prateek
On Nov 5, 1:52 am, Duncan Booth [EMAIL PROTECTED] wrote: Prateek [EMAIL PROTECTED] wrote: I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct

Re: False and 0 in the same dictionary

2008-11-04 Thread bdbull
On Nov 4, 4:21 pm, Prateek [EMAIL PROTECTED] wrote: On Nov 5, 1:52 am, Duncan Booth [EMAIL PROTECTED] wrote: Prateek [EMAIL PROTECTED] wrote: I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this

Re: False and 0 in the same dictionary

2008-11-04 Thread Arnaud Delobelle
Prateek [EMAIL PROTECTED] writes: On Nov 5, 1:52 am, Duncan Booth [EMAIL PROTECTED] wrote: Prateek [EMAIL PROTECTED] wrote: I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a

Re: False and 0 in the same dictionary

2008-11-04 Thread Ben Finney
Prateek [EMAIL PROTECTED] writes: b = {0:'xyz', False:'abc'} b {0: 'abc'} # Am I the only one who thinks this is weird? You're not the only one; I consider this a wart in Python. False and True should be discrete values that don't compare equal with any other value, not even ones that

Re: False and 0 in the same dictionary

2008-11-04 Thread Hendrik van Rooyen
Prateek [EMAIL PROTECTED] wrote: b = {0:'xyz', False:'abc'} b {0: 'abc'} # Am I the only one who thinks this is weird? No. This was discussed before on this list, and that discussion referenced (if I recall correctly) the discussion on python-dev at the time the decision was taken. If