how to mutate a tuple?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carlo v. Dango

    how to mutate a tuple?

    Hello there. I have a function which as an argument takes a tuple and
    either returns that tuple or a mutated version of it. The problem is that
    tuples are imutable, hence I have to create a new tuple and copy the
    content of the old tuple to a new one.

    But how do I do this if I only at runtime know the size of the tuple? I
    wish I could pass around lists instead.. that would be so much easier, but
    I'm passing "*args" and "**kwargs" around so I'm not really the one
    deciding the use of tuples or lists ;)

    Am I the first one with a problem like this? I'm not able to find anything
    using google on this topic. Hope someone can help me ;)

    -Carlo v. Dango


    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
  • Roy Smith

    #2
    Re: how to mutate a tuple?

    "Carlo v. Dango" <oest@soetu.e u> wrote:[color=blue]
    > Hello there. I have a function which as an argument takes a tuple and
    > either returns that tuple or a mutated version of it. The problem is that
    > tuples are imutable, hence I have to create a new tuple and copy the
    > content of the old tuple to a new one.
    >
    > But how do I do this if I only at runtime know the size of the tuple?[/color]

    Copy your input tuple to a list, then say "myTuple = tuple (myList)"

    myList = []
    for item in myInputTuple:
    if this is the item that needs changing:
    item = something else
    myList.append (item)

    return tuple (myList)

    Comment

    • Emile van Sebille

      #3
      Re: how to mutate a tuple?

      Carlo v. Dango asks:[color=blue]
      > But how do I do this if I only at runtime know the size of the tuple? I
      > wish I could pass around lists instead.. that would be so much easier, but
      > I'm passing "*args" and "**kwargs" around so I'm not really the one
      > deciding the use of tuples or lists ;)[/color]

      Convert *args to a list:
      args = list(args)

      kwargs should be a dict anyway, not a tuple.

      Emile van Sebille
      emile@fenx.com


      Comment

      • Duncan Booth

        #4
        Re: how to mutate a tuple?

        "Carlo v. Dango" <oest@soetu.e u> wrote in
        news:oprw1bf9j4 myya52@news.kad net.dom:
        [color=blue]
        > Hello there. I have a function which as an argument takes a tuple and
        > either returns that tuple or a mutated version of it. The problem is
        > that tuples are imutable, hence I have to create a new tuple and copy
        > the content of the old tuple to a new one.
        >
        > But how do I do this if I only at runtime know the size of the tuple?
        > I wish I could pass around lists instead.. that would be so much
        > easier, but I'm passing "*args" and "**kwargs" around so I'm not
        > really the one deciding the use of tuples or lists ;)
        >[/color]

        On entry to your function, convert the original tuple to a list, then
        mutate the list and convert it back to a tuple when returning.

        e.g.
        [color=blue][color=green][color=darkred]
        >>> def ChangeIt(aTuple ):[/color][/color][/color]
        work = list(aTuple)
        work[0] = "Hello"
        return tuple(work)
        [color=blue][color=green][color=darkred]
        >>> ChangeIt((1,2,3 ))[/color][/color][/color]
        ('Hello', 2, 3)[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]


        --
        Duncan Booth duncan@rcp.co.u k
        int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
        "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

        Comment

        • Carlo v. Dango

          #5
          Re: how to mutate a tuple?

          On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <roy@panix.co m> wrote:

          Many thanks for your quick reply!

          aaaahh so there is a tuper() construct! :) that helps a lot.. still this
          seems rather inefficient to create a list and then a new tuple.. but I
          guess there really is no easier way.

          but is there a tuple=>list function ? so that instead of creating the list
          by a for-construct I could just say

          list = tuple.tolist()
          list[0] = newval
          return turple(list)

          ?

          -c.v.d.

          [color=blue]
          > Copy your input tuple to a list, then say "myTuple = tuple (myList)"
          >
          > myList = []
          > for item in myInputTuple:
          > if this is the item that needs changing:
          > item = something else
          > myList.append (item)
          >
          > return tuple (myList)[/color]



          --

          Comment

          • Andrew Bennetts

            #6
            Re: how to mutate a tuple?

            On Tue, Oct 14, 2003 at 03:22:47PM +0200, Carlo v. Dango wrote:[color=blue]
            > Hello there. I have a function which as an argument takes a tuple and
            > either returns that tuple or a mutated version of it. The problem is that
            > tuples are imutable, hence I have to create a new tuple and copy the
            > content of the old tuple to a new one.
            >
            > But how do I do this if I only at runtime know the size of the tuple? I
            > wish I could pass around lists instead.. that would be so much easier, but
            > I'm passing "*args" and "**kwargs" around so I'm not really the one
            > deciding the use of tuples or lists ;)[/color]

            You could always just convert it to a list, and mutate that, e.g.:

            mutable = list(your_tuple )
            mutate(mutable)
            new_tuple = tuple(mutable)

            -Andrew.


            Comment

            • John Roth

              #7
              Re: how to mutate a tuple?


              "Carlo v. Dango" <oest@soetu.e u> wrote in message
              news:oprw1cm4yt myya52@news.kad net.dom...[color=blue]
              > On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <roy@panix.co m> wrote:
              >
              > Many thanks for your quick reply!
              >
              > aaaahh so there is a tuper() construct! :) that helps a lot.. still this
              > seems rather inefficient to create a list and then a new tuple.. but I
              > guess there really is no easier way.
              >
              > but is there a tuple=>list function ? so that instead of creating the list
              > by a for-construct I could just say
              >
              > list = tuple.tolist()
              > list[0] = newval
              > return turple(list)[/color]

              No. If you think about it, for such a method to work, it would have
              to change the object in place from a tuple to a list, otherwise none
              of the bindings would work. If it did it in place, then a tuple
              wouldn't be immutable, would it?

              If you need to mutate a tuple, then I'd begin to question
              whether there isn't something else wrong with the design.

              John Roth[color=blue]
              >
              > ?
              >
              > -c.v.d.
              >
              >[color=green]
              > > Copy your input tuple to a list, then say "myTuple = tuple (myList)"
              > >
              > > myList = []
              > > for item in myInputTuple:
              > > if this is the item that needs changing:
              > > item = something else
              > > myList.append (item)
              > >
              > > return tuple (myList)[/color]
              >
              >
              >
              > --[/color]


              Comment

              • Carlo v. Dango

                #8
                Re: how to mutate a tuple?

                On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <newsgroups@jhr othjr.com>
                wrote:
                [color=blue]
                >
                > "Carlo v. Dango" <oest@soetu.e u> wrote in message
                > news:oprw1cm4yt myya52@news.kad net.dom...[color=green]
                >> On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <roy@panix.co m> wrote:
                >>
                >> Many thanks for your quick reply!
                >>
                >> aaaahh so there is a tuper() construct! :) that helps a lot.. still this
                >> seems rather inefficient to create a list and then a new tuple.. but I
                >> guess there really is no easier way.
                >>
                >> but is there a tuple=>list function ? so that instead of creating the
                >> list
                >> by a for-construct I could just say
                >>
                >> list = tuple.tolist()
                >> list[0] = newval
                >> return turple(list)[/color]
                >
                > No. If you think about it, for such a method to work, it would have
                > to change the object in place from a tuple to a list,[/color]


                sorry, the code should have been: mylist = mytuple.tolist( )

                [color=blue]
                > If you need to mutate a tuple, then I'd begin to question
                > whether there isn't something else wrong with the design.[/color]

                I do.. I'd rather pass around a list.. but as I said I'm using the *args
                mechanism in python which is a tuple and not a list.


                -Carlo van Dango

                Comment

                • Carlo v. Dango

                  #9
                  Re: how to mutate a tuple?

                  On Tue, 14 Oct 2003 23:28:24 +1000, Andrew Bennetts
                  <andrew-pythonlist@puzz ling.org> wrote:

                  many thanks to you all for all the kind replies.. I'm sorry for posting
                  such a simple question.. but I wasn't able to read about tuple() and
                  list() ... without those two its a bit difficult to do what I wanted ;)
                  [color=blue]
                  >
                  > You could always just convert it to a list, and mutate that, e.g.:
                  >
                  > mutable = list(your_tuple )
                  > mutate(mutable)
                  > new_tuple = tuple(mutable)
                  >[/color]



                  --
                  Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

                  Comment

                  • Diez B. Roggisch

                    #10
                    Re: how to mutate a tuple?

                    > but is there a tuple=>list function ? so that instead of creating the list[color=blue]
                    > by a for-construct I could just say
                    >
                    > list = tuple.tolist()
                    > list[0] = newval
                    > return turple(list)[/color]

                    foo = (1,2,3)
                    foo_list = list(foo)

                    Diez

                    Comment

                    • John Roth

                      #11
                      Re: how to mutate a tuple?


                      "Carlo v. Dango" <oest@soetu.e u> wrote in message
                      news:oprw1es0z9 myya52@news.kad net.dom...[color=blue]
                      > On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <newsgroups@jhr othjr.com>
                      > wrote:
                      >[color=green]
                      > >
                      > > "Carlo v. Dango" <oest@soetu.e u> wrote in message
                      > > news:oprw1cm4yt myya52@news.kad net.dom...[color=darkred]
                      > >> On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <roy@panix.co m> wrote:
                      > >>
                      > >> Many thanks for your quick reply!
                      > >>
                      > >> aaaahh so there is a tuper() construct! :) that helps a lot.. still[/color][/color][/color]
                      this[color=blue][color=green][color=darkred]
                      > >> seems rather inefficient to create a list and then a new tuple.. but I
                      > >> guess there really is no easier way.
                      > >>
                      > >> but is there a tuple=>list function ? so that instead of creating the
                      > >> list
                      > >> by a for-construct I could just say
                      > >>
                      > >> list = tuple.tolist()
                      > >> list[0] = newval
                      > >> return turple(list)[/color]
                      > >
                      > > No. If you think about it, for such a method to work, it would have
                      > > to change the object in place from a tuple to a list,[/color]
                      >
                      >
                      > sorry, the code should have been: mylist = mytuple.tolist( )[/color]

                      Ah. That's not a bad idea. Mostly it's a language
                      design decision. object.toSomeOt herType() designs put the
                      responsibility for converting on the 'from' object, languages
                      that overload their constructors (like Python) put the responsibility
                      on the 'to' object. Neither one is clearly better since neither one
                      gracefully handles the question of what to do when you add a new
                      type: there are always places where you can't easily add the
                      needed glue.

                      Python has chosen to go down the path of haveing one obviously
                      'right' way to do most tasks (as distinct from Perl, whose motto is
                      "there's more than one way to do it",) and it's chosen to put the
                      responsibility for type conversion on the constructor of the new type.

                      John Roth

                      [color=blue]
                      >
                      >[color=green]
                      > > If you need to mutate a tuple, then I'd begin to question
                      > > whether there isn't something else wrong with the design.[/color]
                      >
                      > I do.. I'd rather pass around a list.. but as I said I'm using the *args
                      > mechanism in python which is a tuple and not a list.
                      >
                      >
                      > -Carlo van Dango
                      >[/color]


                      Comment

                      Working...