|
>> In the simple sentence above, two int objects were generated. Isn't this slower with python? ?
It’s not obvious whether you’re slow or not, just try it and you’ll know.
>> Could it be slower to modify the value of a variable than to generate a variable? ?
Immutable objects have many benefits, the most obvious is object sharing:
str1 = "abc"
str2 = "abc"
str3 = "abc"
Because "abc" is immutable, now only one "abc" object is needed.
If "abc" is variable, when str1 is modified, str2 and str3 must have a copy of "abc" independent of str1 in order not to be affected, so three objects are required.
And some applications require immutable data, such as dictionary keys. If you have learned hashing, you should be able to understand. |
|