파이썬 문자열은 불변하지 않습니까? 그렇다면 a +“”+ b가 작동하는 이유는 무엇입니까?
내 이해는 Python 문자열은 변경할 수 없다는 것입니다.
다음 코드를 시도했습니다.
a = "Dog"
b = "eats"
c = "treats"
print a, b, c
# Dog eats treats
print a + " " + b + " " + c
# Dog eats treats
print a
# Dog
a = a + " " + b + " " + c
print a
# Dog eats treats
# !!!
파이썬이 할당을 막아야하지 않았습니까? 나는 아마도 뭔가를 놓치고있을 것이다.
어떤 생각?
먼저 a
문자열 "Dog"를 가리 켰습니다. 그런 다음 a
새 문자열 "Dog eats treats"를 가리 키 도록 변수 를 변경했습니다 . 실제로 "Dog"문자열을 변경하지 않았습니다. 문자열은 변경할 수 없으며 변수는 원하는 것을 가리킬 수 있습니다.
문자열 객체 자체는 변경할 수 없습니다.
a
문자열을 가리키는 변수 는 변경 가능합니다.
치다:
a = "Foo"
# a now points to "Foo"
b = a
# b points to the same "Foo" that a points to
a = a + a
# a points to the new string "FooFoo", but b still points to the old "Foo"
print a
print b
# Outputs:
# FooFoo
# Foo
# Observe that b hasn't changed, even though a has.
변수 a는 개체 "개"를 가리 킵니다. Python의 변수를 태그로 생각하는 것이 가장 좋습니다. 당신은 당신이 변화 할 때 당신이 무슨 짓을하는 다른 개체에 태그를 이동할 수 있습니다 a = "dog"
에 a = "dog eats treats"
.
그러나 불변성은 태그가 아니라 객체를 의미합니다.
당신이 노력하면 a[1] = 'z'
할 "dog"
에 "dzg"
, 당신은 오류를 얻을 것이다 :
TypeError: 'str' object does not support item assignment"
문자열은 항목 할당을 지원하지 않기 때문에 변경할 수 없습니다.
메모리 위치 자체를 변경하지 않고 메모리 위치에있는 값을 변경할 수있는 경우에만 무언가 변경 가능합니다.
트릭은 다음과 같습니다. 변경 전후의 메모리 위치가 동일하면 변경 가능합니다.
예를 들어 목록은 변경 가능합니다. 어떻게?
>> a = ['hello']
>> id(a)
139767295067632
# Now let's modify
#1
>> a[0] = "hello new"
>> a
['hello new']
Now that we have changed "a", let's see the location of a
>> id(a)
139767295067632
so it is the same as before. So we mutated a. So list is mutable.
문자열은 변경할 수 없습니다. 어떻게 증명합니까?
> a = "hello"
> a[0]
'h'
# Now let's modify it
> a[0] = 'n'
----------------------------------------------------------------------
우리는 얻는다
그래서 우리는 문자열 변형에 실패했습니다. 그것은 문자열이 불변임을 의미합니다.
재 할당 할 때 새 위치 자체를 가리 키도록 변수를 변경합니다. 여기에서는 문자열을 변경하지 않고 변수 자체를 변경했습니다. 다음은 당신이하는 일입니다.
>> a = "hello"
>> id(a)
139767308749440
>> a ="world"
>> id(a)
139767293625808
id
재 할당 전후가 다르기 때문에 실제로 변이가 아니라 변수를 새 위치로 가리키고 있음을 증명합니다. 그 문자열을 변경하는 것이 아니라 해당 변수를 변경하는 것입니다.
변수는 객체를 가리키는 레이블 일뿐입니다. 개체는 변경할 수 없지만 원하는 경우 레이블이 완전히 다른 개체를 가리 키도록 만들 수 있습니다.
치다:
>>> a='asdf'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x1091aab90>
>>> a='asdf'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x1091aab90>
>>> a='qwer'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x109198490>
같은 값을 변수에 두 번 저장했을 때 16 진수 메모리 위치는 변경되지 않았습니다. 다른 값을 저장하면 변경되었습니다. 문자열은 변경할 수 없습니다. 열광 때문이 아니라 메모리에 새 개체를 만드는 성능 저하를 지불하기 때문입니다. 변수 a
는 해당 메모리 주소를 가리키는 레이블입니다. 무엇이든 가리 키도록 변경할 수 있습니다.
명령문 a = a + " " + b + " " + c
은 포인터를 기반으로 세분화 할 수 있습니다.
a + " "
걸 줄 말한다 a
변경할 수 없습니다 수있는 가리키는 및 추가 " "
내 현재 작업 세트에.
기억:
working_set = "Dog "
a = "Dog"
b = "eats"
c = "treats"
+ b
걸 줄 말한다 b
변경할 수 없습니다 수있는 가리키는 및 현재 작업 세트에 추가합니다.
기억:
working_set = "Dog eats"
a = "Dog"
b = "eats"
c = "treats"
+ " " + c
" "
현재 세트에 추가 를 말합니다 . 그런 다음 c
변경할 수없는 점을 알려주고 현재 작업 세트에 추가하십시오. 기억:
working_set = "Dog eats treats"
a = "Dog"
b = "eats"
c = "treats"
마지막으로 a =
결과 집합을 가리 키도록 포인터를 설정합니다.
기억:
a = "Dog eats treats"
b = "eats"
c = "treats"
"Dog"
더 이상 포인터가 메모리 덩어리에 연결되지 않기 때문에 회수됩니다. 우리는 상주하는 메모리 섹션을 수정하지 않았습니다 "Dog"
. 그러나 메모리의 해당 섹션을 가리키는 레이블 (있는 경우)을 변경할 수 있습니다.
l = [1,2,3]
print id(l)
l.append(4)
print id(l) #object l is the same
a = "dog"
print id(a)
a = "cat"
print id(a) #object a is a new object, previous one is deleted
데이터와 데이터가 연결된 레이블에는 차이가 있습니다. 예를 들어 당신이 할 때
a = "dog"
데이터 "dog"
가 생성되고 레이블 아래에 놓입니다 a
. 레이블은 변경 될 수 있지만 메모리에있는 것은 변경되지 않습니다. 데이터 "dog"
는 수행 한 후에도 여전히 메모리에 존재합니다 (가비지 수집기가 데이터를 삭제할 때까지).
a = "cat"
a
이제 프로그램에서 ^가 ^을 가리 키지 "cat"
만 문자열 "dog"
은 변경되지 않았습니다.
파이썬 문자열은 변경할 수 없습니다. 그러나은 a
문자열이 아닙니다. 문자열 값이있는 변수입니다. 문자열을 변경할 수는 없지만 변수의 값을 새 문자열로 변경할 수 있습니다.
변수는 원하는 위치를 가리킬 수 있습니다. 다음을 수행하면 오류가 발생합니다.
a = "dog"
print a #dog
a[1] = "g" #ERROR!!!!!! STRINGS ARE IMMUTABLE
파이썬 문자열 객체는 변경할 수 없습니다. 예:
>>> a = 'tanim'
>>> 'Address of a is:{}'.format(id(a))
'Address of a is:64281536'
>>> a = 'ahmed'
>>> 'Address of a is:{}'.format(id(a))
'Address of a is:64281600'
이 예제에서 우리는 다른 값을 할당해도 수정되지 않고 새로운 객체가 생성됨을 알 수 있습니다.
그리고 그것은 수정할 수 없습니다. 예:
>>> a[0] = 'c'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
**TypeError**: 'str' object does not support item assignment
오류가 발생합니다.
>>> a = 'dogs'
>>> a.replace('dogs', 'dogs eat treats')
'dogs eat treats'
>>> print a
'dogs'
불변하지 않습니까?!
변수 변경 부분은 이미 논의되었습니다.
귀하의 예 에이 추가를 고려하십시오.
a = "Dog"
b = "eats"
c = "treats"
print (a,b,c)
#Dog eats treats
d = a + " " + b + " " + c
print (a)
#Dog
print (d)
#Dog eats treats
블로그에서 찾은보다 정확한 설명 중 하나는 다음과 같습니다.
In Python, (almost) everything is an object. What we commonly refer to as "variables" in Python are more properly called names. Likewise, "assignment" is really the binding of a name to an object. Each binding has a scope that defines its visibility, usually the block in which the name originates.
Eg:
some_guy = 'Fred'
# ...
some_guy = 'George'
When we later say some_guy = 'George', the string object containing 'Fred' is unaffected. We've just changed the binding of the name some_guy. We haven't, however, changed either the 'Fred' or 'George' string objects. As far as we're concerned, they may live on indefinitely.
Link to blog: https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/
Adding a bit more to above-mentioned answers.
id
of a variable changes upon reassignment.
>>> a = 'initial_string'
>>> id(a)
139982120425648
>>> a = 'new_string'
>>> id(a)
139982120425776
Which means that we have mutated the variable a
to point to a new string. Now there exist two string
(str) objects:
'initial_string'
with id
= 139982120425648
and
'new_string'
with id
= 139982120425776
Consider the below code:
>>> b = 'intitial_string'
>>> id(b)
139982120425648
Now, b
points to the 'initial_string'
and has the same id
as a
had before reassignment.
Thus, the 'intial_string'
has not been mutated.
'mutable' means that we can change the content of the string, 'immutable' means that we can't add an extra string.
Summarizing:
a = 3
b = a
a = 3+2
print b
# 5
Not immutable:
a = 'OOP'
b = a
a = 'p'+a
print b
# OOP
Immutable:
a = [1,2,3]
b = range(len(a))
for i in range(len(a)):
b[i] = a[i]+1
This is an error in Python 3 because it is immutable. And not an error in Python 2 because clearly it is not immutable.
You can make a numpy array immutable and use the first element:
numpyarrayname[0] = "write once"
then:
numpyarrayname.setflags(write=False)
or
numpyarrayname.flags.writeable = False
The built-in function id()
returns the identity of an object as an integer. This integer usually corresponds to the object’s location in memory.
\>>a='dog'
\>>print(id(a))
139831803293008
\>>a=a+'cat'
\>>print(id(a))
139831803293120
Initially, 'a' is stored in 139831803293008 memory location, as the string object is immutable in python if you try to modify and reassign the reference will be removed and will be a pointer to a new memory location(139831803293120).
This image gives the answer. Please read it.
참고URL : https://stackoverflow.com/questions/9097994/arent-python-strings-immutable-then-why-does-a-b-work
'code' 카테고리의 다른 글
LINQ에서 SELECT UNIQUE를 수행하려면 어떻게해야합니까? (0) | 2020.08.30 |
---|---|
NSZombie는 무엇입니까? (0) | 2020.08.29 |
Android의 원형 그래디언트 (0) | 2020.08.29 |
datarow 배열을 데이터 테이블로 변환하는 간단한 방법 (0) | 2020.08.29 |
Android View.getDrawingCache는 null 만 반환합니다. (0) | 2020.08.29 |