Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > str and __setitem__

Reply
Thread Tools

str and __setitem__

 
 
Tor Erik Soenvisen
Guest
Posts: n/a
 
      01-25-2007
Hi,

What do I need to do to make the code below work as expected:

class str2(str):

def __setitem__(self, i, y):
assert type(y) is str
assert type(i) is int
assert i < len(self)

self = self[:i] + y + self[1+i:]


a = str2('123')
a[1] = '1'
print a
123


The print statement should return 113

Regards tores
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-25-2007
Tor Erik Soenvisen wrote:

> What do I need to do to make the code below work as expected:
>
> class str2(str):
>
> ********def __setitem__(self, i, y):
> ****************assert type(y) is str
> ****************assert type(i) is int
> ****************assert i < len(self)
>
> ****************self = self[:i] + y + self[1+i:]


'self' is a local variable; assigning to it rebinds it but has no effect
outside of the __setitem__() method.

> a = str2('123')
> a[1] = '1'
> print a
> 123


> The print statement should return 113


You have to start from scratch as a strings are "immutable" (once created,
their value cannot be changed).

>>> class mutable_str(object):

.... def __init__(self, value):
.... self._value = value
.... def __setitem__(self, index, value):
.... self._value = self._value[:index] + value +
self._value[index+1:]
.... def __str__(self):
.... return self._value
....
>>> a = mutable_str("123")
>>> a[1] = "x"
>>> print a

1x3

Peter
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-25-2007
On Thu, 25 Jan 2007 10:16:31 +0000, Tor Erik Soenvisen wrote:

> Hi,
>
> What do I need to do to make the code below work as expected:


Use another language *wink*

Python strings are immutable, you can't change them in place.

> class str2(str):
>
> def __setitem__(self, i, y):
> assert type(y) is str
> assert type(i) is int
> assert i < len(self)
> self = self[:i] + y + self[1+i:]


This line rebinds a NEW string to the name "self" -- it doesn't change the
contents of the original string. Because the name self is local to the
method, it doesn't change references to the original string.

Are you sure you need mutable strings?

Here are a few different ways of getting something like a mutable string:

* use the MutableString class from the UserString module;

* use the mmap module;

* use lists of characters instead of strings;


--
Steven

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      01-25-2007
Peter Otten:
> >>> class mutable_str(object):... def __init__(self, value):

> ... self._value = value
> ... def __setitem__(self, index, value):
> ... self._value = self._value[:index] + value +
> self._value[index+1:]
> ... def __str__(self):
> ... return self._value
> ...>>> a = mutable_str("123")
> >>> a[1] = "x"
> >>> print a


For this purpose an array may be better, if you have to change it often
and print is only once in a while:

from array import array
a = array("c", "123")
a[1] = "x"
print a

The OP can also use a class, if some other methods are needed:

from array import array

class mutable_str(object):
def __init__(self, value):
self._value = array("c", value)
def __setitem__(self, index, value):
self._value[index] = value
def __str__(self):
return self._value.tostring() # this requires time

a = mutable_str("123")
a[1] = "x"
print a

Probably array.array can be subclassed too...

bye,
bearophile

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between str.isdigit() and str.isdecimal() in Python 3 Marco Python 6 05-17-2012 07:58 AM
bug in str.startswith() and str.endswith() Ethan Furman Python 4 05-27-2011 01:48 PM
*str++ and (*str)++ newbie30 C Programming 0 08-12-2009 04:50 PM
if len(str(a)) == len(str(r)) and isMult(a, r): faster if isMult isslow? maestro Python 1 08-11-2008 01:17 PM
what's the deference between str=null and str=" " ???????? David Java 2 08-03-2003 04:10 PM



Advertisments