VanillaSpinIce
VanillaSpinIce

Reputation: 293

Why does an empty list evaluates to False on a while loop in Python

What is process that occurs for a while-loop to evaluate to False on an empty list ?

For instance:

a=[1, 2, 3]
while a:
    a.pop()

Essentially, I want to know which method or attribute of the list object the while-loop is inspecting in order to decide wether to terminate or not.

Upvotes: 5

Views: 2634

Answers (4)

Mad Physicist
Mad Physicist

Reputation: 114300

Loops and conditionals implicitly use bool on all their conditions. The procedure is documented explicitly in the "Truth Value Testing" section of the docs. For a sequence like a list, this usually ends up being a check of the __len__ method.

bool works like this: first it tries the __bool__ method. If __bool__ is not implemented, it checks if __len__ is nonzero, and if that isn't possible, just returns True.

As with all magic method lookup, Python will only look at the class, never the instance (see Special method lookup). If your question is about how to change the behavior, you will need to subclass. Assigning a single replacement method to an instance dictionary won't work at all.

Upvotes: 9

wizzwizz4
wizzwizz4

Reputation: 6426

Great question! It's inspecting bool(a), which (usually) calls type(a).__bool__(a).

Python implements certain things using "magic methods". Basically, if you've got a data type defined like so:

class MyExampleDataType:
    def __init__(self, val):
        self.val = val

    def __bool__(self):
        return self.val > 20

Then this code will do what it looks like it'll do:

a = MyExampleDataType(5)
b = MyExampleDataType(30)

if a:
    print("Won't print; 5 < 20")
if b:
    print("Will print; 30 > 20")

For more information, see the Python Documentation: 3.3 Special Method Names.

Upvotes: 4

Dominique Paul
Dominique Paul

Reputation: 1751

You can check whether a list is empty by using the bool() function. It evaluates to False if the variable doesn't exist or in the case of a list, when it is empty.

In your case you could do this:

a=[1,2,3]

while bool(a) is True:
    a.pop()

Or even easier:

a = [1,2,3]

while len(a) > 0:

    print(a.pop())

Upvotes: 1

meissner_
meissner_

Reputation: 541

A condition like if my_var is equivalent to if bool(my_var) and this page explains it rather nicely:

Return Value from bool()

The bool() returns:

False if the value is omitted or false True if the value is true

The following values are considered false in Python:

None

False

Zero of any numeric type. For example, 0, 0.0, 0j

Empty sequence. For example, (), [], ''.

Empty mapping. For example, {}

objects of Classes which has bool() or len() method which returns 0 or False

All other values except these values are considered true.

Upvotes: 1

Related Questions