On 19 Apr 2005 01:10:11 -0700, "George Sakkis" <[EMAIL PROTECTED]> wrote:
>Fran=E7ois Pinard wrote: > >> The most useful place for implicit tuple unpacking, in my experience, >> is likely at the left of the `in' keyword in `for' statements (and >> it is even nicer when one avoids extraneous parentheses). > >.=2E. and would be nicest (IMO) if default arguments and *varargs were >allowed too; check http://tinyurl.com/dcb2q for a relevant thread. > You can be a little devious about the left of the 'in' in 'for' statements: ----< tupk.py >------------------------------ class Tupk(object): def _fset(self, arg): # implement unpacking (a, (x, y='default')) self.a = arg[0] if type(arg[1]) is not tuple: # accept (a, x) in place of (a,(x,)) self.x = arg[1] self.y = 'default for non-tuple' else: self.x = arg[1][0] self.y = len(arg[1])==2 and arg[1][1] or 'default' u = property(fset=_fset) def __iter__(self): return iter((self.a, self.x, self.y)) def test(): upk = Tupk() for upk.u in [(1,(2,3)), (4,(5,)), (7,8)]: print upk.a, upk.x, upk.y upk.u = (9,10) a,b,c = upk print 'a=%r, b=%r, c=%r' % (a,b,c) print list(upk), tuple(upk) if __name__=='__main__': test() --------------------------------------------- Output: >>> import tupk >>> tupk.test() 1 2 3 4 5 default 7 8 default for non-tuple a=9, b=10, c='default for non-tuple' [9, 10, 'default for non-tuple'] (9, 10, 'default for non-tuple') >>> You could obviously give Tupk an __init__(fmt, defaults) method that would accept an unpacking spec like 'a, (x, y=%0))', [<default-value 0>] And give its instances a __call__ method so you can use it like a,b,c = upk('a, (x, y=%0))', [555])((7,8)) => a,b,c == (7, 8, 555) How useful how often though? Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list