Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99701 > unrolled thread

What use is this class?

Started byfl <rxjwg98@gmail.com>
First post2015-11-29 13:36 -0800
Last post2015-11-30 01:00 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  What use is this class? fl <rxjwg98@gmail.com> - 2015-11-29 13:36 -0800
    Re: What use is this class? Laura Creighton <lac@openend.se> - 2015-11-30 01:00 +0100

#99701 — What use is this class?

Fromfl <rxjwg98@gmail.com>
Date2015-11-29 13:36 -0800
SubjectWhat use is this class?
Message-ID<783981e2-a996-42f9-ad90-8e90a3bd6492@googlegroups.com>
Hi,

When I search around tutorial about None, I came across this link:

http://jaredgrubb.blogspot.ca/2009/04/python-is-none-vs-none.html

I don't understand what use of this class example:



>>> class Zero(): # a class that is zero
...    def __nonzero__(self):
...       return False


I can only get the following code running:

cz1=Zero()
cz1.__nonzero__()
Out[119]: False


Here are my questions:
1. Is there any other means to use class Zero?
2. What connection to None on the original author's intention?


Thanks,

[toc] | [next] | [standalone]


#99704

FromLaura Creighton <lac@openend.se>
Date2015-11-30 01:00 +0100
Message-ID<mailman.12.1448841620.14615.python-list@python.org>
In reply to#99701
In a message of Sun, 29 Nov 2015 13:36:58 -0800, fl writes:
>Hi,
>
>When I search around tutorial about None, I came across this link:
>
>http://jaredgrubb.blogspot.ca/2009/04/python-is-none-vs-none.html
>
>I don't understand what use of this class example:
>
>
>
>>>> class Zero(): # a class that is zero
>...    def __nonzero__(self):
>...       return False
>
>
>I can only get the following code running:
>
>cz1=Zero()
>cz1.__nonzero__()
>Out[119]: False
>
>
>Here are my questions:
>1. Is there any other means to use class Zero?
>2. What connection to None on the original author's intention?

The person who wrote this was curious as to why PEP 8 says:
"Comparisons to singletons like None should always be done 
 with 'is' or 'is not', never the equality operators."

He wrote this class to play around with, to see if it really made
a difference whether you write:

if x is None:

vs

if x == None:

and it does.  A class is free to implement its own version of '==' if
if likes, and there, 'being equal to None' might means something
complicated and crazy.  People who are just testing against None will
burn their fingers if they use '==' there.

Also its a bad problem when porting code to Jython.
A java null is supposed to be None.
But using '==' will call the underlying .equals method.
If you call that on a null, Java will spit out a Null Pointer Exception.

Laura

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web