[Tutor] Keeping change-in-place vs. copy methods straight

Danny Yoo dyoo at hashcollision.org
Mon Apr 28 22:55:18 CEST 2014


Hi Antonio,


Unfortunately, I don't think it's apparent whether or not a function
applies mutations or is a pure computation.  In Python, those are by
convention or documentation rather than part of the language.  There
are other programming languages can control the effects and scope of
mutation, but Python is not one of those languages.


In particular, I know from experience that list.sort() mutates the
list, and so it doesn't return a useful return value.  On the other
hand, there's a separate built-in function called "sorted()" that can
sort lists, and it does not mutate the original list.

###################################
>>> lst = [3, 1, 4, 1, 5, 9, 2, 6]
>>> lst2 = sorted(lst)
>>> lst
[3, 1, 4, 1, 5, 9, 2, 6]
>>> lst2
[1, 1, 2, 3, 4, 5, 6, 9]
###################################

See: https://docs.python.org/2/library/functions.html#sorted


More information about the Tutor mailing list