laki | 10 Aug 2012 10:20
Favicon

[issue15614] print statement not showing valid result


New submission from laki:

Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
>>> a = [1,2]
>>> b = [2,3]
>>> a.extend(b)
>>> a
[1, 2, 2, 3]
>>> print [1,2].extend([2,3])
None

I did not test this in linux.

----------
components: Windows
messages: 167859
nosy: laki
priority: normal
severity: normal
status: open
title: print statement not showing valid result
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15614>
_______________________________________
_______________________________________________
(Continue reading)

Martin v. Löwis | 10 Aug 2012 10:58
Favicon

[issue15614] print statement not showing valid result


Martin v. Löwis added the comment:

This is not a bug. extend is a procedure with a side effect: the "self" object (i.e. "a" in your example) is modified.

By convention, procedures return None in Python, as opposed to functions, which have no side effect but
return a result. This is to avoid code like

def combine(a, b):
  return a.extend(b)

a = ...
b = ...
c = combine(a,b)

If extend would return the "self" list, then people may think that they get a fresh, new list, and then wonder
why a is modified.

IOW: your bug report is actually invalid; the result that print shows is exactly the right result that
extend returns.

----------
nosy: +loewis
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report <at> bugs.python.org>
<http://bugs.python.org/issue15614>
_______________________________________
(Continue reading)


Gmane