2016-10-26 10 views
-1

[:]を使用すると値のコピーであり、使用せずに参照を取得し値を変更していますか?単純なarr1 = arr2とarr1 = arr2 [:]の違いは何ですか?

+0

を - 私はこれはおそらく優れていると思いますOPの質問は必ずしも特定のリストではないので、dupe:http://stackoverflow.com/q/6167238/748858 – mgilson

+0

@mgilson - \ * shrug \ *確かに、私にとってはうまくいくようです。あなたが好きなときは自由に開いて再閉鎖してください。 – TigerhawkT3

答えて

-1

これは、スライスされるオブジェクトに非常に依存します。 よく[:]あなたのシャローコピーできます:

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 

をそれだけで、オブジェクト自体を返す病理学的オブジェクト書くのは簡単です:TigerhawkT3 @

class SliceMe(object): 
    def __getitem__(self, obj): 
     return self 
関連する問題