Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python why would you use [:] over =

Tags:

I am just learning python and I am going though the tutorials on https://developers.google.com/edu/python/strings

Under the String Slices section

s[:] is 'Hello' -- omitting both always gives us a copy of the whole thing (this is the pythonic way to copy a sequence like a string or list)

Out of curiosity why wouldn't you just use an = operator?

s = 'hello'; bar = s[:]  foo = s  

As far as I can tell both bar and foo have the same value.

like image 570
Steven Smethurst Avatar asked Jan 21 '13 06:01

Steven Smethurst


People also ask

What does [:] do in Python?

A shortcut way to copy a list or a string is to use the slice operator [:] . This will make a shallow copy of the original list keeping all object references the same in the copied list.

How do I check if equals in Python?

Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.

How do you use equals in Python?

Python Equal To (==) OperatorThe equal to operator returns True if the values on either side of the operator are equal. As we know, 3 is an integer, and '3' is a string. Hence, they're unequal.


1 Answers

= makes a reference, by using [:] you create a copy. For strings, which are immutable, this doesn't really matter, but for lists etc. it is crucial.

>>> s = 'hello' >>> t1 = s >>> t2 = s[:] >>> print s, t1, t2 hello hello hello >>> s = 'good bye' >>> print s, t1, t2 good bye hello hello 

but:

>>> li1 = [1,2] >>> li = [1,2] >>> li1 = li >>> li2 = li[:] >>> print li, li1, li2 [1, 2] [1, 2] [1, 2] >>> li[0] = 0 >>> print li, li1, li2 [0, 2] [0, 2] [1, 2] 

So why use it when dealing with strings? The built-in strings are immutable, but whenever you write a library function expecting a string, a user might give you something that "looks like a string" and "behaves like a string", but is a custom type. This type might be mutable, so it's better to take care of that.

Such a type might look like:

class MutableString(object):     def __init__(self, s):         self._characters = [c for c in s]      def __str__(self):         return "".join(self._characters)      def __repr__(self):         return "MutableString(\"%s\")" % str(self)      def __getattr__(self, name):         return str(self).__getattribute__(name)      def __len__(self):         return len(self._characters)      def __getitem__(self, index):         return self._characters[index]      def __setitem__(self, index, value):         self._characters[index] = value      def __getslice__(self, start, end=-1, stride=1):         return str(self)[start:end:stride]   if __name__ == "__main__":     m = MutableString("Hello")     print m     print len(m)     print m.find("o")     print m.find("x")     print m.replace("e", "a") #translate to german ;-)     print m     print m[3]     m[1] = "a"     print m     print m[:]      copy1 = m     copy2 = m[:]     print m, copy1, copy2     m[1] = "X"     print m, copy1, copy2 

Disclaimer: This is just a sample to show how it could work and to motivate the use of [:]. It is untested, incomplete and probably horribly performant

like image 149
Thorsten Kranz Avatar answered Jan 10 '23 17:01

Thorsten Kranz