Idiom gone, or did it really ever exist? () is ()

Mike C. Fletcher mcfletch at home.com
Tue Apr 17 23:59:43 EDT 2001


Over the years, I've occasionally used this idiom:

NULLARGUMENT = ()

def someFunctionOrMethod  (argument = NULLARGUMENT ):
    if argument is NULLARGUMENT:
        doSomething()
    else:
        doSomethingElse()

That is, I was attempting to distinguish between a call where argument is
passed a NULL tuple and a call where argument is passed nothing at all.
When I was working on unit tests for my current piece of code, however, I
discovered that this no longer works (Python 1.5.2).  No idea for how long
it hasn't worked.  Now, of course, I could have used:

class NULL: pass
NULLARGUMENT = NULL() # a particular instance

or

NULLARGUMENT = NULL # use the class itself

or

NULLARGUMENT = []

or I could've used an approach where I used *arguments and **namedarguments
to determine whether the argument was really passed.  There are all sorts of
other ways to do this.  Apparently optimisation has come to the tuples of
the world, so using tuple identities is now a no-no.

All those who have used null-tuple identities should now go and fix their
code.
Mike

__________________________________
 Mike C. Fletcher
 Designer, VR Plumber
 http://members.home.com/mcfletch





More information about the Python-list mailing list