[Tutor] lists, name semantics

Ben Finney ben+python at benfinney.id.au
Sat Apr 18 05:27:55 CEST 2015


Bill Allen <wallenpb at gmail.com> writes:

> If I have a list defined as my_list = ['a','b','c'], what is the is
> differnce between refering to it as my_list or my_list[:]?

‘my_list’ is a reference to the object you've already described (the
existing object ‘['a', 'b', 'c']’).

‘my_list[:]’ is an operation that takes the original object and creates
a new one by slicing. In this case, the new object happens to be equal
to (but probably not identical to) the original, because of the slice
you specified.

> Is there any nuance I am missing here? Situations where one form
> should be used as opposed to the other?

You need to understand, when writing code, whether you are intending to
refer to the original object, or to create a new one. Neither is better,
they are both common but different operations.

-- 
 \          “Our products just aren't engineered for security.” —Brian |
  `\             Valentine, senior vice-president of Microsoft Windows |
_o__)                                                development, 2002 |
Ben Finney



More information about the Tutor mailing list