Python Basic Doubt

Chris Angelico rosuav at gmail.com
Sat Aug 10 18:09:02 EDT 2013


On Sat, Aug 10, 2013 at 10:48 PM, Gary Herron
<gary.herron at islandtraining.com> wrote:
> This is an oversimplification, but generally useful for all beginner (and
> most advanced) programmers:
>     Don't use "is" for comparisons.  Use "==".
> It 20 years of programming Python, I've *needed* to use "is" ... only once
> or twice.

Hrm, I wouldn't make it that hard a rule. Both comparisons have their
place. As has been mentioned earlier in this thread, checking if
something is None is spelled "if something is None". Checking if it
equals zero is spelled "if it == 0", which is a quite different check.
The other common check that uses 'is' is with an argument default
where absolutely anything could be passed:

_notpassed = object()
def frob(appendage, device=_notpassed):
    """Use some appendage to frob some device, or None to frob nothing.
    Omit device to frob whatever is currently held in that appendage"""
    if device is _notpassed:
        device = ...  # whatever you need
    if device is not None:
        # frob the device

But granted, equality comparisons are a LOT more common than identity
comparisons.

ChrisA



More information about the Python-list mailing list