str and __setitem__

Discussion in 'Python' started by Tor Erik Soenvisen, Jan 25, 2007.

  1. 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

    The print statement should return 113

    Regards tores
    Tor Erik Soenvisen, Jan 25, 2007
    1. Advertisements

  2. Tor Erik Soenvisen

    Peter Otten Guest

    'self' is a local variable; assigning to it rebinds it but has no effect
    outside of the __setitem__() method.
    You have to start from scratch as a strings are "immutable" (once created,
    their value cannot be changed).
    .... def __init__(self, value):
    .... self._value = value
    .... def __setitem__(self, index, value):
    .... self._value = self._value[:index] + value +
    .... def __str__(self):
    .... return self._value

    Peter Otten, Jan 25, 2007
    1. Advertisements

  3. Use another language *wink*

    Python strings are immutable, you can't change them in place.
    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 D'Aprano, Jan 25, 2007
  4. Peter Otten:
    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...

    bearophileHUGS, Jan 25, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.