Re: Test None for an object that does not implement ==

2011-12-26 Thread Ethan Furman
Devin Jeanpierre wrote: Um -- if you don't want a and c being passed in, why put them in the function signature? He wants both or neither to be passed in. Ah -- right. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list

Re: Test None for an object that does not implement ==

2011-12-26 Thread Paul Rudin
GZ writes: > I run into a weird problem. I have a piece of code that looks like the > following: > > f(, a=None, c=None): > assert (a==None)==(c==None) > There is only one 'None' - so use 'a is None' rather than 'a == None'. (In common lisp there is a particular language construct tha

Re: Test None for an object that does not implement ==

2011-12-26 Thread Roy Smith
In article , Devin Jeanpierre wrote: > > Um -- if you don't want a and c being passed in, why put them in the > > function signature? > > He wants both or neither to be passed in. assert sum(foo is None for foo in [a, c]) % 2 == 0 -- http://mail.python.org/mailman/listinfo/python-list

Re: Test None for an object that does not implement ==

2011-12-25 Thread Devin Jeanpierre
> Um -- if you don't want a and c being passed in, why put them in the > function signature? He wants both or neither to be passed in. -- Devin On Sun, Dec 25, 2011 at 11:27 PM, Ethan Furman wrote: > GZ wrote: >> >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like th

Re: Test None for an object that does not implement ==

2011-12-25 Thread Ethan Furman
Nobody wrote: nothing should compare equal to None except for None itself, so "x is None" and "x == None" shouldn't produce different results unless there's a bug in the comparison method. Why wouldn't you want other types that can compare equal to None? It could be useful for a Null type to

Re: Test None for an object that does not implement ==

2011-12-25 Thread Ethan Furman
GZ wrote: Hi, I run into a weird problem. I have a piece of code that looks like the following: f(, a=None, c=None): assert (a==None)==(c==None) Um -- if you don't want a and c being passed in, why put them in the function signature? ~Ethan~ -- http://mail.python.org/mailman/list

Re: Test None for an object that does not implement ==

2011-12-25 Thread Ian Kelly
On Sun, Dec 25, 2011 at 2:38 AM, Nobody wrote: > On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote: > >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(, a=None, c=None): >>     assert  (a==None)==(c==None) >> >> >> The problem is that == is not impleme

Re: Test None for an object that does not implement ==

2011-12-25 Thread Devin Jeanpierre
> Which of course leads to a SillyPEP for a new keyword, "are", which > would allow you to write: > a and c are None > > instead of the much more verbose > a is None and c is None How about: >>> a is b is None ;) -- Devin On Sun, Dec 25, 2011 at 7:27 PM, Roy Smith wrote: > In ar

Re: Test None for an object that does not implement ==

2011-12-25 Thread Steven D'Aprano
On Sun, 25 Dec 2011 15:45:10 -0800, Larry Hudson wrote: > On 12/24/2011 11:09 PM, GZ wrote: >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(, a=None, c=None): >> assert (a==None)==(c==None) >> > <...> > > At first glance this loo

Re: Test None for an object that does not implement ==

2011-12-25 Thread Christian Heimes
Am 25.12.2011 15:04, schrieb Chris Angelico: > I think there are certain types that are actually not implemented as > classes, and hence cannot be subclassed. This is almost certainly an > implementation detail though; my testing was done in Py3.2 (on Windows > fwiw). Some extension types are not

Re: Test None for an object that does not implement ==

2011-12-25 Thread Roy Smith
In article , Devin Jeanpierre wrote: > The issue here is that "== None" is being used instead of "is None", > but I believe that's been covered. Your response doesn't include it, > so maybe it's worth restating. Which of course leads to a SillyPEP for a new keyword, "are", which would allow yo

Re: Test None for an object that does not implement ==

2011-12-25 Thread Devin Jeanpierre
> At first glance this looked like it should be a simple boolean "and", but > then I realized that when a and c are both unequal to None, the result would > also be True. This implies the logical approach would be exclusive-or (^). Among booleans, "!=" is exclusive or and "==" is its negation. I

Re: Test None for an object that does not implement ==

2011-12-25 Thread Larry Hudson
On 12/24/2011 11:09 PM, GZ wrote: Hi, I run into a weird problem. I have a piece of code that looks like the following: f(, a=None, c=None): assert (a==None)==(c==None) <...> At first glance this looked like it should be a simple boolean "and", but then I realized that when a and

Re: Test None for an object that does not implement ==

2011-12-25 Thread Lie Ryan
On 12/26/2011 01:13 AM, Roy Smith wrote: In article, Chris Angelico wrote: On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: Just for fun, I tried playing around with subclassing NoneType and writing an __eq__ for my subclass. Turns out, you can't do that: Traceback (most recent call la

Re: Test None for an object that does not implement ==

2011-12-25 Thread Chris Angelico
On Mon, Dec 26, 2011 at 1:13 AM, Roy Smith wrote: > If you were designing the interface from scratch, you would probably > represent that with an exception hierarchy Or possibly with "returns a False value", giving the option of None for none available, False for none will ever be available. Of c

Re: Test None for an object that does not implement ==

2011-12-25 Thread Roy Smith
In article , Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: > > Just for fun, I tried playing around with subclassing NoneType and > > writing an __eq__ for my subclass.  Turns out, you can't do that: > > > > Traceback (most recent call last): > >  File "./none.py",

Re: Test None for an object that does not implement ==

2011-12-25 Thread Chris Angelico
On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano wrote: > I can't think of any other un-subclassable classes other than NoneType. > Which ones are you thinking of? I don't remember, but it was mentioned in a thread a little while ago. Experimentation shows that 'bool' is one of them, though. Thi

Re: Test None for an object that does not implement ==

2011-12-25 Thread Steven D'Aprano
On Mon, 26 Dec 2011 00:35:46 +1100, Chris Angelico wrote: [...] >> TypeError: Error when calling the metaclass bases >>    type 'NoneType' is not an acceptable base type > > Yes; unfortunately quite a few Python built-in classes can't be > subclassed. I can't think of any other un-subclassable

Re: Test None for an object that does not implement ==

2011-12-25 Thread Chris Angelico
On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: > Just for fun, I tried playing around with subclassing NoneType and > writing an __eq__ for my subclass.  Turns out, you can't do that: > > Traceback (most recent call last): >  File "./none.py", line 5, in >    class Nihil(NoneType): > TypeErro

Re: Test None for an object that does not implement ==

2011-12-25 Thread Roy Smith
In article , Lie Ryan wrote: > Now, whether doing something like that is advisable or not, that's a > different question; however nothing in python states that you couldn't > have something that compare equal to None whether there is a bug or not > in the comparison method. Just for fun, I t

Re: Test None for an object that does not implement ==

2011-12-25 Thread Lie Ryan
On 12/25/2011 08:38 PM, Nobody wrote: nothing should compare equal to None except for None itself, so "x is None" > and "x == None" shouldn't produce different results unless there's a > bug in the comparison method. not necessarily, for example: import random class OddClass: def __eq__(s

Re: Test None for an object that does not implement ==

2011-12-25 Thread Nobody
On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote: > I run into a weird problem. I have a piece of code that looks like the > following: > > f(, a=None, c=None): > assert (a==None)==(c==None) > > > The problem is that == is not implemented sometimes for values in a > and c, causing an excep

Re: Test None for an object that does not implement ==

2011-12-24 Thread Paul Rubin
GZ writes: > assert (a==None)==(c==None)... > So how do I reliably test if a value is None or not? Equality is the wrong comparison to start with. Use "a is None". -- http://mail.python.org/mailman/listinfo/python-list

Test None for an object that does not implement ==

2011-12-24 Thread GZ
Hi, I run into a weird problem. I have a piece of code that looks like the following: f(, a=None, c=None): assert (a==None)==(c==None) The problem is that == is not implemented sometimes for values in a and c, causing an exception NotImplementedError. I ended up doing assert (not a)==