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


Groups > comp.lang.python > #99704

Re: What use is this class?

From Laura Creighton <lac@openend.se>
Newsgroups comp.lang.python
Subject Re: What use is this class?
Date 2015-11-30 01:00 +0100
Message-ID <mailman.12.1448841620.14615.python-list@python.org> (permalink)
References <783981e2-a996-42f9-ad90-8e90a3bd6492@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web