By diciu


2009-11-05 18:24:09 8 Comments

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

7 comments

@DummyHead 2018-04-06 08:33:08

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.

@Rafael Aguilar 2018-01-31 20:57:56

The list.append function does returns None, it just add the value to the list you are using to call that method.

Here is something to make things clearer:

>>> u = []
>>> not u
False
>>> print(u.append(6)) # u.append(6) == None
None    
>>> not u.append(6) # not None == True
True

@Alex Martelli 2009-11-05 18:29:40

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).

@diciu 2009-11-05 18:56:34

I bumped into the issue while writing (y.append(5) and (yield y)) where I didn't expect a successful list addition to (indirectly) evaluate to false.

@adam 2009-11-05 18:29:13

Actually, it returns None


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

@SilentGhost 2009-11-05 18:25:40

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'

@Matt Anderson 2009-11-05 18:25:28

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

@Matthieu M. 2009-11-05 18:25: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 :)

@S.Lott 2009-11-05 18:41:47

Mutators (like append, extend, sort, etc.) which update a list do not return a value.

@SwiftsNamesake 2016-11-19 14:23:02

None doesn't evaluate to False.

@Chris_Rands 2017-02-15 09:49:59

@SwiftsNamesake Try bool(None)

@SwiftsNamesake 2017-02-15 10:18:00

@Chris_Rands 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.

@Chris_Rands 2017-02-15 10:33:51

@SwiftsNamesake No-one said None is a boolean, but it does evaluate to False

@SwiftsNamesake 2017-02-15 10:40:52

@Chris_Rands 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'.

@Matthieu M. 2017-02-15 10:45:41

@SwiftsNamesake: I am open to suggestions, evaluates remains the best expression I have so far.

@Chris_Rands 2017-02-15 11:00:57

@SwiftsNamesake 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 meaning

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How do I return multiple values from a function?

17 Answered Questions

[SOLVED] Running shell command and capturing the output

14 Answered Questions

[SOLVED] Parsing boolean values with argparse

31 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

8 Answered Questions

[SOLVED] Why does "not(True) in [False, True]" return False?

14 Answered Questions

25 Answered Questions

[SOLVED] Why not inherit from List<T>?

3 Answered Questions

[SOLVED] Why does Python code run faster in a function?

1 Answered Questions

[SOLVED] Why does (1 in [1,0] == True) evaluate to False?

2 Answered Questions

[SOLVED] Why python evaluates 0 to False?

  • 2013-05-31 18:24:48
  • yeyo
  • 391 View
  • -1 Score
  • 2 Answer
  • Tags:   python

Sponsored Content