|
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/ | |
Share:
|
"Carlo v. Dango" <oe**@soetu.eu> wrote: 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) | | |
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 em***@fenx.com | | |
"Carlo v. Dango" <oe**@soetu.eu> wrote in
news:op**************@news.kadnet.dom: 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. def ChangeIt(aTuple):
work = list(aTuple)
work[0] = "Hello"
return tuple(work)
ChangeIt((1,2,3))
('Hello', 2, 3)
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure? | | |
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> 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. 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)
-- | | |
On Tue, Oct 14, 2003 at 03:22:47PM +0200, Carlo v. Dango wrote: 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. | | |
"Carlo v. Dango" <oe**@soetu.eu> wrote in message
news:op**************@news.kadnet.dom... On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> 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)
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) -- | | |
On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <ne********@jhrothjr.com>
wrote: "Carlo v. Dango" <oe**@soetu.eu> wrote in message news:op**************@news.kadnet.dom... On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> 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) 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 | | |
On Tue, 14 Oct 2003 23:28:24 +1000, Andrew Bennetts
<an***************@puzzling.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 ;) You could always just convert it to a list, and mutate that, e.g.:
mutable = list(your_tuple) mutate(mutable) new_tuple = tuple(mutable)
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ | | |
> 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 | | |
"Carlo v. Dango" <oe**@soetu.eu> wrote in message
news:op**************@news.kadnet.dom... On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <ne********@jhrothjr.com> wrote:
"Carlo v. Dango" <oe**@soetu.eu> wrote in message news:op**************@news.kadnet.dom... On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> 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) 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
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 | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
9 posts
views
Thread by Yomanium Yoth Taripoät II |
last post: by
|
50 posts
views
Thread by Will McGugan |
last post: by
|
1 post
views
Thread by fedor |
last post: by
|
15 posts
views
Thread by Steve M |
last post: by
|
37 posts
views
Thread by Gregor Horvath |
last post: by
|
77 posts
views
Thread by Nick Maclaren |
last post: by
|
2 posts
views
Thread by Alan Isaac |
last post: by
|
4 posts
views
Thread by Alex Vinokur |
last post: by
|
reply
views
Thread by Hatem Nassrat |
last post: by
| | | | | | | | | | |