How can I test if a reference is null ?

Alex Martelli aleax at aleax.it
Fri Mar 28 12:47:10 CET 2003


Greg Ewing (using news.cis.dfn.de) wrote:

> Alex Martelli wrote:
>> cudjoe wrote:
>>>I know that there are no types in python, but how can we test if a
>>>variable is empty ?
>> 
>> 
>> I believe the test you want is
>> 
>>     if icon is not None:
> 
> Just as an aside, there isn't really a true equivalent
> of NULL in Python -- all references always point at
> *some* object. None happens to be a built-in object
> provided for references to point at when they don't
> want to point to anything else.

Philosophically important and valid distinction, but
the OP was asking specifically about "null references"
in Jython (as was made clear from text snipped here),
so the question was about what strategy Jython uses
to map Java 'null' to Python, and I _think_ (can't
check -- gotta rebuild Jython, mysteriously broken right
now on my machine) it uses None for that.

As for whether "all references always point at *some*
object", one might quibble:

>>> def f(): print c; c=23
...
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in f
UnboundLocalError: local variable 'c' referenced before assignment
>>>

the reference that is local variable 'c' might usefully
be said to point at NO object in this case (it's "unbound",
i.e., not [yet] BOUND to "point to" ANY object).  This is
not relevant to the OP's question, but it might be to
your more general observation.  I cannot imagine somebody
wanting to test for this, but if they did I think it would
have to be with a try/except.


Alex





More information about the Python-list mailing list