-1

Please explain why c = None in the following Python 3 example, from the Python command line.

>>> a = [1,2]
>>> b = a
>>> c = b.append(3)
>>> print(a,b,c)
[1, 2, 3] [1, 2, 3] None
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
user12711
  • 693
  • 1
  • 7
  • 21
  • 2
    You can and you're doing it right, the result of this particular function call happens to be `None`. – RemcoGerlich Dec 31 '16 at 21:26
  • Your comment clarifies it for me. BTW, I see this question is marked "Duplicate" However, it looks different in nature to me, since I was asking why a variable couldn't be set to equal a function call. That other question linked in above as the original, didn't have anything to do with trying to set a variable to be equal to a function call. (and I bet there are lots of other questions with an answer of "because the function returned "None"... but that's just my biased opinion, since I wrote the question I suppose I'm overly partial to it. – user12711 Dec 31 '16 at 21:46

3 Answers3

5

list.append() appends the entry in place and returns nothing which Python takes as None (default behavior of Python). For example:

>>> def foo():
...     print "I am in Foo()"
...     # returns nothing
...
>>> c = foo()   # function call
I am in Foo()
>>> c == None
True   # treated as true
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Would that be expected in most other programming languages, especially Perl, Ruby, Java, C++? Or does it vary language by language? – user12711 Dec 31 '16 at 21:27
  • @user12711 It is language dependent, to be more precise *the way the complier is written*. Some will give syntax error if you will try to assign a variable to function that returns nothing whereas some won't. To give the exact list of languages that does that, I should be knowing the each language, which I unfortunately don't know :) – Moinuddin Quadri Dec 31 '16 at 21:30
  • So instead of being equal to the new value of b, it's equal to the return value of the function (which is None). And, a = the new b, because lists (and strings) are mutable and therefore one. – user12711 Dec 31 '16 at 21:55
  • It has nothing to do with the mutability of the object. You are making a function call i.e `list.append()` which returns nothing, resulting in value of `c` as `None`. And the other part i.e. `b==a` is `True` because when you did `b = a`, it didn't created a new object, but the reference to instance of `a` is copied to b as well. In short, both `a` and `b` are refering same object – Moinuddin Quadri Dec 31 '16 at 22:01
  • That clarifies it! – user12711 Dec 31 '16 at 23:53
4

You are assigning the return value of append which is None.

>>> a = [1,2]
>>> b = a.append(3)
>>> b == None
True
>>>
helloV
  • 50,176
  • 7
  • 137
  • 145
3

The function append doesn't return anything, that's why you have none in the variable. Let's see it better with a little example:

Let's say you have this function

def foo:
    bar = "Return this string" 
    return bar 

x = foo() 
print(x) # x will be "Return this string" 

Now let's say you have this function instead

def foo(bar):
    print(bar) 

x = foo(33) # Here 33 will be printed to the console, but x will be None 

This happens because the return statement on the function, if you don't have any, the function will return None.

append is a function to do something in the list that you're calling it in (in Python strings are lists too), and this function doesn't need to return anything, it only modifies the list.