10

I'm translating a Python code to C in order to take advantage of the parallelism available on HPC systems. The original programmer used a conditional in Python that confuses me:

if rnum <> current_res:
 alim = 0
 if len(f): alim = f[-1]

What does if len(f) satisfy? I cannot find this convention used in this way anywhere online. I imagine it is a bad programming practice.

mkrieger1
24.2k7 gold badges68 silver badges84 bronze badges
asked Apr 9, 2013 at 14:53
1
  • 11
    <> has long since been deprecated. Use != instead. Commented Apr 9, 2013 at 14:53

3 Answers 3

29

In Python, values that are considered 'empty', such as numeric 0, are considered False in a boolean context, and otherwise are True.

Thus, len(f) is True if f has length, otherwise it is False. If f is a standard Python sequence type then the code can be simplified to:

if f: alim = f[-1]

because the same applies to sequences; empty is False, having a non-zero length means it's True *.

See Truth Value Testing in the Python documentation for all the important details.

Note that <> has been deprecated; you should really use != instead.


*: Note that doesn't necessarily apply to non-builtin sequence types, only those that implement __bool__ in this way.

answered Apr 9, 2013 at 14:54
Sign up to request clarification or add additional context in comments.

5 Comments

Is it safe to assume that len(f) holds the same truth value as f? Surely it depends on f
@Awalias: If it is a custom sequence type then it is not safe to make that assumption.
@Awalias -- if f implements a custom __nonzero__ (__bool__ py3k) then they could be different, but that's a rare case.
It depends, of course, if f is a class I created and I override len to return 4... it won't be the same. But then again I should be beaten with a stick if I did.
@pcalcao -- If you've not done anything with __nonzero__ it will be the same.
12

In most cases if len(f): will be the same thing as if f:...

the reason is because if f has no length, it will return 0 which is a falsy value. Actually, truth testing actually checks the length of an object (__len__) under some circumstances.

answered Apr 9, 2013 at 14:54

Comments

9

This

if len(f)

would translate to:

if len(f) != 0

or in this case, since len(f) is never negative,

if len(f) > 0
answered Apr 9, 2013 at 14:57

10 Comments

Strictly speaking, only != 0, but since you can't have a negative length sequence, it's not functionally relevant.
@sr2222, I've clarified the answer. They are equivalent here, since the example is len, and I included it because len > 0 is more readable.
@sr2222 -- But could you have a negative length other object? The docs say the result should be an integer >= 0, but I don't know that means that it's enforced or just expected...
-0. The pythonic idiom would be if f:.
@mgilson heh, in the unexpected case I suppose len > 0 is the best option :P
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.