Message17910
Not sure how crucial this exactly is, but the
itertools.izip() can be abused to create a tuple that
can be mutated once. I couldn't find a way to crash
anything, however, but I wouldn't leave such a
possibility open in the Python interpreter nevertheless.
. from itertools import imap, izip
.
. def mutatingtuple(tuple1, f, tuple2):
. # this builds a tuple t which is a copy of tuple1,
. # then calls f(t), then mutates t to be equal to
tuple2
. # (needs len(tuple1) == len(tuple2)).
. def g(value, first=[1]):
. if first:
. del first[:]
. f(z.next())
. return value
. items = list(tuple2)
. items[1:1] = list(tuple1)
. gen = imap(g, items)
. z = izip(*[gen]*len(tuple1))
. z.next()
.
. def f(t):
. global T
. T = t
. print T
.
. mutatingtuple((1,2,3), f, (4,5,6)) # print T -> (1, 2, 3)
. print T # print T -> (4, 5, 6)
|
|
Date |
User |
Action |
Args |
2007-08-23 14:16:20 | admin | link | issue793826 messages |
2007-08-23 14:16:20 | admin | create | |
|