On Jul 7, 1:45 am, ssecorp <[EMAIL PROTECTED]> wrote:
> >>> h = "aja baja"
# 'aja baja' is created and assigned to the name h
> >>> h += 'e'
# Is the equivalent of:
# h = h + 'e'
#
# In there, a h and 'e' is concatenated and assigned to
# a new string object which is bound to h. The
# original str
> so why would you ever want mutability?
>
>
> seems very counterintuitive and unreliable.
For lists, mutability is fairly natural.
Suppose you have a function f that copies
some items from one list to another. You write it
as
def f(src, dst):
for x in src:
if condition(x):
ssecorp wrote:
> so why would you ever want mutability?
>
>
> seems very counterintuitive and unreliable.
Because immutability imposes a lot of restrictions and performance
characteristics that mutable objects don't have. For example, compare
building up a list and a tuple element-by-element (u
so why would you ever want mutability?
seems very counterintuitive and unreliable.
--
http://mail.python.org/mailman/listinfo/python-list
"ssecorp" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
so if strings were mutable and i did
a = b = "foo"
and then did
a += "bar"
then a and b would be foobar?
This can be demonstrated with a list of characters, which *is* mutable:
a = b = list('foo')
a += list('bar')
a
['f'
so if strings were mutable and i did
a = b = "foo"
and then did
a += "bar"
then a and b would be foobar?
--
http://mail.python.org/mailman/listinfo/python-list
Peter Otten wrote:
ssecorp wrote:
h = "aja baja"
h += 'e'
h
'aja bajae'
The inplace-add operator doesn't mutate the lvalue, it just rebinds it:
In Python, neither '=' nor members of the 'op=' family are operators.
They all mark *assignment* or *augmented assignment* statements that
*all
ssecorp wrote:
h = "aja baja"
h += 'e'
h
> 'aja bajae'
What Peter said, or, to put it another way:
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = b = "
ssecorp wrote:
h = "aja baja"
h += 'e'
h
> 'aja bajae'
The inplace-add operator doesn't mutate the lvalue, it just rebinds it:
>>> a = b = "foo"
>>> id(a)
47643036142016
>>> a += "bar"
>>> id(a), a
(47643036142064, 'foobar')
>>> id(b), b
(47643036142016, 'foo')
Peter
--
http