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
    123


    The print statement should return 113

    Regards tores
    Tor Erik Soenvisen, Jan 25, 2007
    #1
    1. Advertising

  2. Tor Erik Soenvisen

    Peter Otten Guest

    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
    Peter Otten, Jan 25, 2007
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Jan 25, 2007
    #3
  4. Tor Erik Soenvisen

    Guest

    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
    , Jan 25, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    477
    Thomas G. Marshall
    Aug 3, 2003
  2. maestro
    Replies:
    1
    Views:
    304
    Chris
    Aug 11, 2008
  3. newbie30

    *str++ and (*str)++

    newbie30, Aug 12, 2009, in forum: C Programming
    Replies:
    0
    Views:
    745
    newbie30
    Aug 12, 2009
  4. Ethan Furman
    Replies:
    4
    Views:
    248
    Roy Smith
    May 27, 2011
  5. Marco
    Replies:
    6
    Views:
    1,379
    Marco
    May 17, 2012
Loading...

Share This Page