how to mutate a tuple?

C

Carlo v. Dango

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
 
A

Andrew Bennetts

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 ;)

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

Roy Smith

Carlo v. Dango said:
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?

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)
 
D

Duncan Booth

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 ;)

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.
work = list(aTuple)
work[0] = "Hello"
return tuple(work)
 
E

Emile van Sebille

Carlo v. Dango asks:
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 ;)

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

kwargs should be a dict anyway, not a tuple.

Emile van Sebille
(e-mail address removed)
 
C

Carlo v. Dango

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.

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)



--
 
J

John Roth

Carlo v. Dango said:
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)

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
?

-c.v.d.

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)



--
 
C

Carlo v. Dango

Carlo v. Dango said:
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)

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,


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

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

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
 
C

Carlo v. Dango

On Tue, 14 Oct 2003 23:28:24 +1000, Andrew Bennetts

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 ;)
 
D

Diez B. Roggisch

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)

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

Diez
 
J

John Roth

Carlo v. Dango said:
Carlo v. Dango said:
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)

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,


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

Ah. That's not a bad idea. Mostly it's a language
design decision. object.toSomeOtherType() 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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top