Re: [Tutor] t = (1, *(2, 3))
Jabin Jezreel wrote: > I am not allowed to do t = (1, *(2, 3)) > > But I am allowed to do def ts(*t): > ...return t > ... ts(1, *(2, 3)) > (1, 2, 3) > > I realize I can do (1,) + (2,3) > (1, 2, 3) > > What is the rationale behind not having t = (1, *(2, 3)) > have the same semantics as the "ts" case above? They are different things. However, I think a proposal to make the above work sounds like a good thing to bring up on the python-ideas list. Stefan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
> > >>> x = (2, 3) > > >>> y = (1, *x) > > File "", line 1 > > SyntaxError: can use starred expression only as assignment target > > But you can already do that without needing to extend * notation > to work like that To me the "*" notation already /appears/ to work like that: (Python 3.x) >>> a = [1, 2, 3] >>> (b, *c) = a >>> b 1 >>> c [2, 3] Just only in one direction. >>> a = (b, *c) File "", line 1 SyntaxError: can use starred expression only as assignment target Larry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
On Thu, May 14, 2009 at 11:10:53AM -0700, Jabin Jezreel wrote: > > Why not just write is simply as (1, 2, 3) instead of > > the confusing (1, *(2, 3))? > > It is a contrived example. In practice it would be > something more like: > > >>> def ts(*t): > ... return t > ... > >>> x = (2, 3) > >>> y = (1, *x) > File "", line 1 > SyntaxError: can use starred expression only as assignment target > >>> y = ts(1, *x) > >>> y > (1, 2, 3) But you can already do that without needing to extend * notation to work like that, so in the Pythonic spirit of there only being one obvious/best/clear way to do something... y = (1,) + x why does that operation have to be constructed as y = (1, *x)? > > > > Don't say that (2, 3) might be a variable, it > > won't work without breaking python object model. > > If such construct creates a new tuple, it would > > need to break python's object model [...] > > Break how? > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
> Why not just write is simply as (1, 2, 3) instead of > the confusing (1, *(2, 3))? It is a contrived example. In practice it would be something more like: >>> def ts(*t): ... return t ... >>> x = (2, 3) >>> y = (1, *x) File "", line 1 SyntaxError: can use starred expression only as assignment target >>> y = ts(1, *x) >>> y (1, 2, 3) > Don't say that (2, 3) might be a variable, it > won't work without breaking python object model. > If such construct creates a new tuple, it would > need to break python's object model [...] Break how? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
Steve Willoughby wrote: Lie Ryan wrote: Jabin Jezreel wrote: I am not allowed to do t = (1, *(2, 3)) But I am allowed to do def ts(*t): return t ts(1, *(2, 3)) (1, 2, 3) I realize I can do (1,) + (2,3) (1, 2, 3) What is the rationale behind not having t = (1, *(2, 3)) have the same semantics as the "ts" case above? I guess because it is not clear what (1, *(2, 3)) should mean. Parentheses when used for function call has different semantic then when parentheses is used for tuple syntax. Parentheses in function is part of the calling syntax, while parentheses in tuple is used only for grouping. I'm not sure that's a strong argument against allowing "unfolding" tuples in any expression beyond function calls, though, but it is how Python works. PS: anyway I just realized that since tuple is immutable, having (1, *(2, 3)) be (1, 2, 3) would violate immutability. Maybe a list would be a better example. No, it doesn't. This would be the expression for constructing a new tuple, which would, after that point, be immutable. What's the point? Why not just write is simply as (1, 2, 3) instead of the confusing (1, *(2, 3))? Don't say that (2, 3) might be a variable, it won't work without breaking python object model. If such construct creates a new tuple, it would need to break python's object model, unless you make it as an special case. Python tend to avoid special case, unless it is really, really necessary. Anyway, I think whatever good reason you have for this syntax, the BDFL will just say no. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
Lie Ryan wrote: > Jabin Jezreel wrote: >> I am not allowed to do > t = (1, *(2, 3)) >> >> But I am allowed to do > def ts(*t): >> return t >> > ts(1, *(2, 3)) >> (1, 2, 3) >> >> I realize I can do > (1,) + (2,3) >> (1, 2, 3) >> >> What is the rationale behind not having t = (1, *(2, 3)) >> have the same semantics as the "ts" case above? > I guess because it is not clear what (1, *(2, 3)) should mean. > Parentheses when used for function call has different semantic then when > parentheses is used for tuple syntax. Parentheses in function is part of > the calling syntax, while parentheses in tuple is used only for grouping. I'm not sure that's a strong argument against allowing "unfolding" tuples in any expression beyond function calls, though, but it is how Python works. > PS: anyway I just realized that since tuple is immutable, having (1, > *(2, 3)) be (1, 2, 3) would violate immutability. Maybe a list would be > a better example. No, it doesn't. This would be the expression for constructing a new tuple, which would, after that point, be immutable. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
Jabin Jezreel wrote: I am not allowed to do t = (1, *(2, 3)) But I am allowed to do def ts(*t): return t ts(1, *(2, 3)) (1, 2, 3) I realize I can do (1,) + (2,3) (1, 2, 3) What is the rationale behind not having t = (1, *(2, 3)) have the same semantics as the "ts" case above? In the face of ambiguity, refuse the temptation to guess? I guess because it is not clear what (1, *(2, 3)) should mean. Parentheses when used for function call has different semantic then when parentheses is used for tuple syntax. Parentheses in function is part of the calling syntax, while parentheses in tuple is used only for grouping. PS: anyway I just realized that since tuple is immutable, having (1, *(2, 3)) be (1, 2, 3) would violate immutability. Maybe a list would be a better example. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] t = (1, *(2, 3))
"Jabin Jezreel" wrote I am not allowed to do >>> t = (1, *(2, 3)) Just to be clear, what do you think this means? What would you expect to happen? But I am allowed to do >>> def ts(*t): ... return t ... >>> ts(1, *(2, 3)) (1, 2, 3) What do you think is happening here that is different? I realize I can do >>> (1,) + (2,3) (1, 2, 3) And how does this relate to the use of the asterisk? Is this what you expect the asterisk version above to do? What is the rationale behind not having t = (1, *(2, 3)) have the same semantics as the "ts" case above? That I don't know. The asterisk notation can only be used for unpacking function arguments, but why it is limited to that role I don't know...? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor