if instance exists problem ..

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Oct 17 19:53:22 EDT 2007


"Diez B. Roggisch" <deets at nospam.web.de> writes:

> stef mientki schrieb:
> > What should I do to the same simple test for existance ?
> 
> Use isinstance(obj, type).

No, that's *far* more specific than "does it exist", and will give
false negatives.

Much better is::

    foo = None
    foo = do_something_to_get_instance()
    # ...
    if foo is not None:
        # foo was bound to an instance correctly

Even better is just to use the object, and if it's not what was
expected, catch the exception at a level that knows what to do about
it.

-- 
 \      "At my lemonade stand I used to give the first glass away free |
  `\          and charge five dollars for the second glass. The refill |
_o__)                         contained the antidote."  -- Emo Philips |
Ben Finney



More information about the Python-list mailing list