Difference between 'is' and '=='

Max M maxm at mxm.dk
Wed Mar 29 12:01:18 CEST 2006


Joel Hedlund wrote:
>> There's no requirement that the socket module or
>> anything else return values using the same object that the
>> socket.AF_UNIX constant uses.
> 
> 
> Ouch. That's certainly an eyeopener.
> 
> For me, this means several things, and I'd really like to hear people's 
> thoughts about them.
> 
> It basically boils down to "don't ever use 'is' unless pushed into a 
> corner, and nevermind what PEP8 says about it".



Identity checks are often used for checking input parameters of a function:

def somefunc(val=None):
     if val is None:
         val = []
     do_stuff(val)



Or if None is a possible parameter you can use your own object as a marker::

_marker = []

def somefunc(val=_marker):
     if val is marker:
         val = []
     do_stuff(val)



-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone:  +45 66 11 84 94
Mobile: +45 29 93 42 96



More information about the Python-list mailing list