Issue 1714448: if something as x: (original) (raw)

Created on 2007-05-07 17:09 by k0wax, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
assexp.diff jdwhitley,2009-03-14 13:30 Inline Assignment Expression Py3k
Messages (13)
msg55105 - (view) Author: k0wax (k0wax) Date: 2007-05-07 17:09
--- if map[x][y].overlay: map[x][y].overlay.blit(x,y) --- ... and ... --- if map[x][y].overpay as ob: ob.blit(x, y) --- the second one looks much more fun I think.
msg55106 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-05-18 02:12
Toss your idea out on python-ideas. It isn't horrible. And it helps with while-statement issues.
msg55107 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-05-18 05:57
FWIW, I have a patch for it. :)
msg83587 - (view) Author: Jervis Whitley (jdwhitley) Date: 2009-03-14 13:30
Hi, I like this idea. I've put together a short patch that will implement inline assignment. if f() -> name: use(name) or more powerfully: if f() -> name == 'spam': usespam(name) the old syntax if something as x: is still available if that is what is desired. if (f() == 'spam') -> name: newname = name.replace('p', 'h') Patched against Py3k please kick the tires, I've added some tests and developed a PEP.
msg83608 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2009-03-15 00:30
What's wrong with this? ob = map[x][y].overpay if ob: ob.blit(x, y) Is this proposal just about saving one line? If we allow this, how many of the following will be allowed? if expr as name: while expr as name: expr as name # alternative to "name = expr" Frankly, the only one that seems to be useful to me is the second. As for using "->", please no, there are plenty of languages that use line noise, but Python doesn't need to be one of them.
msg83610 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2009-03-15 00:37
Regarding the proposed syntax: if (f() == 'spam') -> name: newname = name.replace('p', 'h') Surely that should assign the *bool* result of comparing f() with 'spam' to name? Doing anything else is opening the door to a world of pain. if (f() == 'spam') -> name # binds name=f() if ('spam' == f()) -> name # binds 'spam' to name? if (f() == g()) -> name # binds what to name? if (f() or g()) -> name # binds what to name?
msg83611 - (view) Author: Jervis Whitley (jdwhitley) Date: 2009-03-15 00:42
> If we allow this, how many of the following will be allowed? > if expr as name: > while expr as name: > expr as name # alternative to "name = expr" This patch implements your final point: expr as name (albeit with a nominal '->' RARROW rather than 'as') the patch creates a new expression, assexp (assignment expression) there is no need to implement this for countless other if/while/for because they accept expressions and this assignment is an expression. (Note it is a patch for a different behaviour than the OP suggested.) > As for using "->", please no, there are plenty of languages that use > line noise, but Python doesn't need to be one of them. I have begun a discussion about this on python-ideas to give it some air as suggested by Raymond. We can always close the issue as 'wont fix' if it doesn't get off the ground. This issue (although addressing an old concern dating back to the beginning of python) has been sitting unloved for 9 or so months and I felt that we should at least resolve it. Cheers, Jervis
msg83613 - (view) Author: Jervis Whitley (jdwhitley) Date: 2009-03-15 00:47
> Regarding the proposed syntax: > if (f() == 'spam') -> name: > newname = name.replace('p', 'h') > Surely that should assign the *bool* result of comparing f() > with 'spam' to name? Doing anything else is opening the door to a > world of pain. You are correct. It does assign the result of the bool. I have made an error in creating the example. This is what happens when I copy and paste and don't check the result. should read if f -> name: # use name, (pointless example but in line with the OP's suggestion) Thanks for picking this up.
msg83614 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2009-03-15 02:45
At the moment binding occurs either right-to-left with "=", eg. x = y where "x" is the new name, or left-to-right, eg. import x as y where "y" is the new name. If the order is to be right-to-left then using "as" seems to be the best choice. On the other hand, if there should be a form of binding explicitly for use in an expression in order to prevent accidental use of "=" then the order should probably be the same as "=", ie right-to-left, and a new symbol is needed (using punctuation feels preferable somehow, because "=" uses punctuation). The only symbol I can think of is "~=". How does this: if ob ~= map[x][y].overpay: ob.blit(x, y) look compared to: if map[x][y].overpay as ob: ob.blit(x, y) IMHO, of course.
msg83618 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2009-03-15 04:38
Matthew suggested ~= instead of -> or "as". I dislike this because ~= first makes me think of "approximately equal to", and then it makes me think of augmented assignment, and only then do I remember that although ~ is used in Python for bitwise-not, ~= is not a legal augmented assignment.
msg83620 - (view) Author: Jervis Whitley (jdwhitley) Date: 2009-03-15 05:58
> Matthew suggested ~= instead of -> or "as". Try the patch, you can make changes (for those that aren't aware) by changing the token in Grammar/Grammar to whatever you wish. It is easy to do and you need only recompile after this step. example: assexp: xor_expr ['->' xor_expr] could become assexp: xor_expr ['magic' xor_expr] >>> 'hello' magic words 'hello' >>> words 'hello' Note that Mr Barnett may need to look at other fixes to get his '~=' idea off the ground (tokenizer.c and specifically adding a new token) I've recommended that we close this issue. Cheers, Jervis
msg83622 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2009-03-15 07:13
Rejecting this after discussion on python-ideas: http://mail.python.org/pipermail/python-ideas/2009-March/003423.html Overview of some of the major objections here: http://mail.python.org/pipermail/python-ideas/2009-March/003440.html
msg83637 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2009-03-15 15:39
Just for the record, I wasn't happy with "~=" either, and I have no problem with just forgetting the whole idea.
History
Date User Action Args
2022-04-11 14:56:24 admin set github: 44940
2009-03-15 15:39:24 mrabarnett set messages: +
2009-03-15 07:13:07 ncoghlan set status: open -> closednosy: + ncoghlanmessages: + resolution: rejected
2009-03-15 05:58:30 jdwhitley set messages: +
2009-03-15 04:38:02 steven.daprano set messages: +
2009-03-15 02:45:39 mrabarnett set nosy: + mrabarnettmessages: +
2009-03-15 00:47:42 jdwhitley set messages: +
2009-03-15 00:42:50 jdwhitley set messages: +
2009-03-15 00:37:53 steven.daprano set messages: +
2009-03-15 00:30:18 steven.daprano set nosy: + steven.dapranomessages: +
2009-03-14 13:30:12 jdwhitley set files: + assexp.diffversions: + Python 3.1, - Python 2.6nosy: + jdwhitleymessages: + keywords: + patch
2007-05-07 17:09:08 k0wax create