2

What does it mean to say if n? I don't get why if n works in an if statement. Shouldn't there be an argument such as if n == 0 or something, not just if n?

def AddMusicAtPosition(self, newMusic, n):
    if n:
        self.nextMusic.AddMusicAtPosition(newMusic, n - 1)
    else:
        newMusic.nextMusic = self.nextMusic
        self.nextMusic = newMusic

4 Answers 4

10

In Python, if n is equivalent to if bool(n).

For integers, bool(i) equals to i != 0.


If n is an instance of a class, then

  • if the class defines __bool__, then n.__bool__() is called
  • if the class doesn't define __bool__ but __len__, then n.__len__() != 0 is evaluated
  • if the class defines neither __bool__ nor __len__, it always evaluates to True (think like n is not None).
2
  • 1
    "If n is a class object, then" isn't quite right. Perhaps "If n is an instance of a class, then" Dec 25, 2018 at 8:06
  • @RoadRunner Well, I think the point was to avoid confusion with instances of type and its subclasses, which are objects that represent classes and thus could reasonably be called "class objects".
    – David Z
    Dec 25, 2018 at 8:11
0

Usually conditions are like:

if n==1:

But that evaluates to:

if True:

If the conditions is right, python in default everything is True, so the statement is passed, and since:

bool(n)

Could be True or False too, n could do it already, and the code will pass if n is True, don't pass if n is False, so that's why it works.

0

Any value in Python can be tested for truth. As long as it's not None, False, zero, or empty; it is considered true. See more details in the documentation.

In your case the recursion should stop when n gets to zero as zero is not considered True. You can test this with:

if 0:
  print('zero is true') # won't be printed
else:
  print('zero is false') # will be printed
3
  • 1
    I don't think it's fair to say "As long as it's not None, False, zero, or empty; it is considered true", because that ignores all the custom types that define __bool__() or __len__().
    – David Z
    Dec 25, 2018 at 8:04
  • I was trying to keep it simple and not go too much over what the question actually asks. if __len__() returns 0, for example, you can arguably call it empty.
    – kichik
    Dec 25, 2018 at 8:08
  • 1
    Yeah, that's reasonable, although it still ignores the case of __bool__(), which I think is a big enough exception that it's worth mentioning. The fact is, there are a lot of values which are not None, False, zero, nor could be considered "empty" in any meaningful sense, and yet are falsy because of their class's implementation of __bool__(). I completely agree with not getting into the details, I just think it's important to mention that what you've said doesn't apply to all values.
    – David Z
    Dec 25, 2018 at 8:10
0

In Python, almost everything is True except False, None, 0 and Empty entities (string, list, sets, dictionary)!(Could have miss others)

So if you state that:

if n:
    print(True)

If n is not 0, False, None or Empty entities, then it would print True.

To test the values and behavior, you can do:

ns = [None, 0, False, '', {},(), [],1, True,]

for n in ns:
    if n:
        print(n, 'It is True')
    else:
        print(n, 'it is False')

So what happens is that if n, checks for truthfulness of value n :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.