Why does list.append evaluate to false in a boolean context? [duplicate]
Asked Answered
A

7

37

Is there a reason being list.append evaluating to false? Or is it just the C convention of returning 0 when successful that comes into play?

>>> u = []
>>> not u.append(6)
True
Almire answered 5/11, 2009 at 18:24 Comment(8)
A possibly better way to phrase: why does python not use the Builder pattern .. so we can do u.append(6).append(7). .. This is annoying.Silicious
@javadba Why not just do u.extend((6, 7))?Heeling
@Stefan append() and extend() have different effects and are not interchangeableSilicious
@javadba What difference? My extend has the same effect as your two appends.Heeling
extend() retains a single list if the elements were a list whereas append() will be a list of lists. In the above case they are not so it's a special case that does end up the same between extend() and append().Silicious
@javadba Not sure what you mean, but it sounds wrong. It doesn't matter that we used numbers. If we use lists instead, my extend still has the same effect as your two appends.Heeling
@StefanPochmann difference between append() and extend() #253203Silicious
@javadba Not sure what that shall tell me. I know the difference. But you seem mistaken about extend.Heeling
R
45

Most Python methods that mutate a container in-place return None -- an application of the principle of Command-query separation. (Python's always reasonably pragmatic about things, so a few mutators do return a usable value when getting it otherwise would be expensive or a mess -- the pop method is a good example of this pragmatism -- but those are definitely the exception, not the rule, and there's no reason to make append an exception).

Richmal answered 5/11, 2009 at 18:29 Comment(0)
L
20

None evaluates to False and in python a function that does not return anything is assumed to have returned None.

If you type:

>> print u.append(6)
None

Tadaaam :)

Lassitude answered 5/11, 2009 at 18:25 Comment(8)
Mutators (like append, extend, sort, etc.) which update a list do not return a value.Bales
None doesn't evaluate to False.Upanishad
@Upanishad Try bool(None)Natale
@Natale None is not a boolean, which is why you need the bool function to convert it (done implicitly by not). Yes, I know it's a quibble but it's True.Upanishad
@Upanishad No-one said None is a boolean, but it does evaluate to FalseNatale
@Natale I guess my quibble is with the phrase evaluates to, which admittedly is due to the OP. As I said I don't dispute the gist of the answer (who would) and I know that None is 'falsy'.Upanishad
@SwiftsNamesake: I am open to suggestions, evaluates remains the best expression I have so far.Lassitude
@Upanishad I find evaluates clearer than Python's own semantics, help(bool) says "bool(x): Returns True when the argument x is true". But x is true seems too similar to x is True, which of course has a completely different meaningNatale
L
7

because .append method returns None, therefore not None evaluates to True. Python on error usually raises an error:

>>> a = ()
>>> a.append(5)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    a.append(5)
AttributeError: 'tuple' object has no attribute 'append'
Lanfri answered 5/11, 2009 at 18:25 Comment(0)
E
5

It modifies the list in-place, and returns None. None evaluates to false.

Efflux answered 5/11, 2009 at 18:25 Comment(0)
L
3

Actually, it returns None


>>> print u.append(6)
None
>>> print not None
True
>>> 

Leshalesher answered 5/11, 2009 at 18:29 Comment(0)
E
1

Method append modifies the list in-place and the return value None

In your case, you are creating an array — [6] — on the fly, then discarding it. The variable b ends up with the return value of None.

Why?
This comply with the principle of Command–query separation devised by Bertrand Meyer.
It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In your example:

u.append(6)

append modified the state of [], so it’s not a best practice to return a value compliance with the principle.

In theoretical terms, this establishes a measure of sanity, whereby one can reason about a program's state without simultaneously modifying that state.

CQS is well-suited to the object-oriented methodology such as python.

Electrolier answered 6/4, 2018 at 8:33 Comment(1)
This also destroys the builder() pattern. It is one of the many reasons python is difficult to use.Silicious
P
0

The list.append function returns None. It just adds the value to the list you are calling the method from.

Here is something that'll make things clearer:

>>> u = []
>>> not u
False
>>> print(u.append(6)) # u.append(6) == None
None    
>>> not u.append(6) # not None == True
True
Popish answered 31/1, 2018 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.