Sujet : Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)
De : list1 (at) *nospam* tompassin.net (Thomas Passin)
Groupes : comp.lang.pythonDate : 08. Nov 2024, 23:00:31
Autres entêtes
Message-ID : <mailman.89.1731115813.4695.python-list@python.org>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 11/8/2024 2:09 PM, dn via Python-list wrote:
On 8/11/24 14:40, Mild Shock via Python-list wrote:
Well you can use your Browser, since
JavaScript understand post and pre increment:
Question: are we talking Python or JavaScript?
So we have x ++ equals in Python:
Trying to find a word-for-word translation serves as badly in computer- programming languages as it does in human spoken-languages. Learn how to adapt and embrace the differences...
x + = 1
x - 1
The above probably only 'works' (the way you expect) in the REPL.
But I don't know how to combine an
assignment and an expression into one
expession. In JavaScript one can use
Again!
"Everything should be made as simple as possible, but no simpler."
Check out "The Zen of Python" and PEP-0008 for Python idioms.
the comma:
>
> x = 5
5
> y = (x += 1, x - 1)
5
> x = 5
5
> y = (x += 1, x)
6
>
But in Python the comma would create a tuple.
Exactly, just as driving on the left side of the road will be fine in some countries but cause a crash in others. Learn the local rules FIRST!
The 'walrus operator' could be applied:
>>> x = 5
>>> y = (x := x + 1); x
6
>>> x, y
(6, 6)
However, if such were submitted for Code Review, unhappiness would result.
Was the question re-phrased to: how to ... in Python, we'd end-up with something more like this:
>>> x = 5 # define
>>> x += 1 # increment
>>> y = x # alias
>>> x, y
(6, 6)
Or, still Pythonic but simpler:
>>> x = 5
>>> y = x = x + 1
>>> x, y
(6, 6)