when I use [:] is it copy of values and without using it is it taking the reference and changing the value?
Asked
Active
Viewed 347 times
-1
-
@TigerhawkT3 -- I think this is probably a better dupe since OP's question isn't necessarily list specific: http://stackoverflow.com/q/6167238/748858 – mgilson Oct 26 '16 at 00:08
-
@mgilson - \*shrug\* Sure, seems fine to me. Feel free to open and reclose if you like. – TigerhawkT3 Oct 26 '16 at 00:09
1 Answers
-1
This depends critically on the object being sliced. Frequently, [:] gives you a shallow copy:
a = [1, 2, 3]
b = a[:]
# `b` is distinct from `a`, but `b` references the same objects that `a` does.
print(b is a) # False
print(all(x is y for x, y in zip(a, b)) # True
but it's easy to write a pathological object which just returns the object itself:
class SliceMe(object):
def __getitem__(self, obj):
return self
mgilson
- 283,004
- 58
- 591
- 667