Two aces up Python's sleeve (Posting On Python-List Prohibited)
dn
PythonList at DancesWithMice.info
Fri Nov 8 14:09:49 EST 2024
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)
--
Regards,
=dn
More information about the Python-list
mailing list