|
Prev: Problem with subprocess.Popen wget within a thread
Next: interpretation of special characters in Python
From: ssecorp on 6 Jul 2008 14:45 >>> h = "aja baja" >>> h += 'e' >>> h 'aja bajae' >>>
From: Peter Otten on 6 Jul 2008 14:57 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
From: Mel on 6 Jul 2008 16:36 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 = "aja baja" >>> a += "e" >>> print a aja bajae >>> print b aja baja Mutability/immutability makes a difference in programs when different symbols (or container items) share a value. Mel.
From: Terry Reedy on 6 Jul 2008 16:50 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* bind objects to targets. Augmented assignments are, rather obviously, restricted to binding one object to one target. For 'x op= y', if the object originally bound to x is mutable, the arithmetic operation part of the augmented assignment can (should) be implemented by an inplace __i<opname>__ special method that (normally, but not necessarily) mutates and returns self to be rebound. Otherwise, the interpreter calls the normal __<opname>__ special method (if it exits) that returns a new object to be bound. Thus, '+=' is neither an operator nor is the indicated operation necessarily inplace. Immutable built-in classes do not have __i<opname>__ methods. So given that name h is bound to a string, h += 'e' has exactly the same effect as h = h + 'e' which has exactly the same effect as h = h.__add__('e') The same is true for immutable instances of other built-in classes. Terry Jan Reedy
From: ssecorp on 6 Jul 2008 17:33 so if strings were mutable and i did a = b = "foo" and then did a += "bar" then a and b would be foobar?
|
Next
|
Last
Pages: 1 2 Prev: Problem with subprocess.Popen wget within a thread Next: interpretation of special characters in Python |