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 ...
--
http://mail.python.org/mail
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
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
alf <[EMAIL PROTECTED]> 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:
Sentine
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 a
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 fast
> 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, amo
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 exp
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 y