testing for valid reference: obj vs. None!=obs vs. obj is not None

Discussion in 'Python' started by alf, Sep 4, 2006.

  1. alf

    alf Guest

    Hi,

    I have a reference to certain objects. What is the most pythonic way to
    test for valid reference:

    if obj:

    if None!=obs:

    if obj is not None:

    --
    alfz1
     
    alf, Sep 4, 2006
    #1
    1. Advertising

  2. alf

    Carl Banks Guest

    alf wrote:
    > Hi,
    >
    > I have a reference to certain objects. What is the most pythonic way to
    > test for valid reference:
    >
    > if obj:
    >
    > if None!=obs:
    >
    > if obj is not None:


    If you're checking whether an object is None or not, the third is the
    best way.

    Some people might say you should use the first: this works sometimes,
    but sometimes an object that is not None can be false, in which case
    this test will fail. (I'll give you an example of one place where it
    bit me: in ElementTree. An Element is false if there are no
    subelements, thus to test whether a certain element exist, you can't
    just say "if aaa.find('bbb')", because a bbb tag exists, it is false
    unless it has subelements of its own. You must instead say "if
    aaa.find('bbb') is not None".)

    The second test is slower than the first and adds nothing. Whether
    something is None is an identity test; identity tests should use is.


    Carl Banks
     
    Carl Banks, Sep 4, 2006
    #2
    1. Advertising

  3. Re: testing for valid reference: obj vs. None!=obs vs. obj is notNone

    alf a écrit :
    > Hi,
    >
    > I have a reference to certain objects. What is the most pythonic way to
    > test for valid reference:
    >
    > if obj:


    Don't do this:

    for o in [0, '', [], {}, ()]:
    print obj, bool(obj), obj is None


    > if None!=obs:


    In python, assignement is a statement, not an expression, so there's no
    way you could write 'if obj = None' by mistake (-> syntax error). So
    this style is unpythonic. Also, None is a singleton, and identity test
    is way faster than equality test.

    > if obj is not None:


    That's the good one.
     
    Bruno Desthuilliers, Sep 5, 2006
    #3
  4. > I have a reference to certain objects. What is the most pythonic way to
    > test for valid reference:
    >
    > if obj:
    >
    > if None!=obs:
    >
    > if obj is not None:


    The third way is the most precise way. It is often used in combination
    with default arguments.

    def __init__(self, amount = None):
    if amount is not None:
    self.amount = amount
    else:
    self.amount = self.calc_amount()

    However, the first way is shorter and more concise. It really depends
    on what obj is supposed to be and where you get obj from. If it is a
    database and obj is an instance:

    obj = db.get("sometable", id = 33)

    (assuming db.get returns None if the object isn't found) Then "if
    obj:" clearly is the right test. In general, I think "If obj:" is the
    right answer, except when dealing with default arguments. And you must
    be aware that some objects, like 0, [] or {} evaluate to False -- it
    is possible that that could create some subtle bugs.

    --
    mvh Björn
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Sep 5, 2006
    #4
  5. alf

    Sandra-24 Guest

    alf wrote:
    > Hi,
    >
    > I have a reference to certain objects. What is the most pythonic way to
    > test for valid reference:
    >
    > if obj:
    >
    > if None!=obs:
    >
    > if obj is not None:


    I like this way the most. I used timeit to benchmark this against the
    first one, expecting it to be faster (the first is a general false
    test, the last should just be comparing pointers) but it's slower.
    Still I don't expect that to ever matter, so I use it wherever I wish
    to test for None, it reads the best of all of them.

    -Sandra
     
    Sandra-24, Sep 5, 2006
    #5
  6. alf

    Carl Banks Guest

    Bruno Desthuilliers wrote:
    > In python, assignement is a statement, not an expression, so there's no
    > way you could write 'if obj = None' by mistake (-> syntax error). So
    > this style is unpythonic. Also, None is a singleton, and identity test
    > is way faster than equality test.


    Playing Devil's advocate here: if you were to write "x!=None", then x's
    __eq__ method is invoked, which might not account for the possibility
    that the other operand is None.

    However, if you write "None!=x", then None's __eq__ method is invoked,
    which accounts for any given type.


    Carl Banks
     
    Carl Banks, Sep 5, 2006
    #6
  7. alf

    Paul Rubin Guest

    alf <ask@me> writes:
    > I have a reference to certain objects. What is the most pythonic way
    > to test for valid reference:


    If you're intending to use None as a sentinel for an invalid reference,
    then use
    > if obj is not None:


    You could also make a unique sentinel:

    Sentinel = object()
    ...
    if obj is not Sentinel: ...

    "if obj" isn't necessarily what you want; obj might be a valid but
    empty container.

    "if obj != None" is also wrong, for reasons someone else explained.
     
    Paul Rubin, Sep 5, 2006
    #7
  8. Re: testing for valid reference: obj vs. None!=obs vs. obj is notNone

    Carl Banks wrote:
    > Bruno Desthuilliers wrote:
    >> In python, assignement is a statement, not an expression, so there's no
    >> way you could write 'if obj = None' by mistake (-> syntax error). So
    >> this style is unpythonic. Also, None is a singleton, and identity test
    >> is way faster than equality test.

    >
    > Playing Devil's advocate here: if you were to write "x!=None", then x's
    > __eq__ method is invoked, which might not account for the possibility
    > that the other operand is None.
    >
    > However, if you write "None!=x", then None's __eq__ method is invoked,
    > which accounts for any given type.
    >


    Well... Since we all know it should be an identity test anyway... <g>



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Sep 5, 2006
    #8
  9. At Monday 4/9/2006 17:02, alf wrote:

    >I have a reference to certain objects. What is the most pythonic way to
    >test for valid reference:


    By "valid reference" you mean, you have initially:
    obj = None
    and you want to detect whether obj is bound to another, different,
    object, right?

    > if obj:


    This checks whether obj is considered True, not whether it is None.
    e.g. obj=[] would not pass.

    > if None!=obs:


    Would be OK, but the next one is better:

    > if obj is not None:


    This is what you are looking for! :)



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Sep 12, 2006
    #9
  10. alf

    alf Guest

    Re: testing for valid reference: obj vs. None!=obs vs. obj is notNone

    alf wrote:
    > Hi,
    >
    > I have a reference to certain objects. What is the most pythonic way to
    > test for valid reference:
    >
    > if obj:
    >
    > if None!=obs:
    >
    > if obj is not None:
    >

    thx for all answers - now "if obj is not None:" in an obvious choice ...
     
    alf, Dec 9, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shalabh Chaturvedi
    Replies:
    2
    Views:
    460
    Mike C. Fletcher
    Feb 20, 2004
  2. Martin Corino
    Replies:
    0
    Views:
    112
    Martin Corino
    May 3, 2011
  3. length power
    Replies:
    2
    Views:
    114
    Rustom Mody
    Apr 10, 2014
  4. Skip Montanaro
    Replies:
    0
    Views:
    80
    Skip Montanaro
    Apr 10, 2014
  5. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    70
    Johannes Schneider
    Apr 10, 2014
Loading...

Share This Page