• Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)

    From Lawrence D'Oliveiro@ldo@nz.invalid to comp.lang.python on Fri Nov 8 01:10:34 2024
    From Newsgroup: comp.lang.python

    On Thu, 07 Nov 2024 12:55:53 +0530, Annada Behera wrote:

    I heard this behavior is because python's integers are immutable.

    Nothing to do with that.

    ++x or x++ will redefine 5 to 6, which the interpreter forbids ...

    One of those is actually syntactically valid.

    It just won’t do what you expect it to do.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Mild Shock@janburse@fastmail.fm to comp.lang.python on Fri Nov 8 02:40:56 2024
    From Newsgroup: comp.lang.python

    Well you can use your Browser, since
    JavaScript understand post and pre increment:

    x = 5
    5
    x ++
    5
    x = 5
    5
    ++ x
    6

    So we have x ++ equals in Python:

    x + = 1
    x - 1

    And ++ x equals in Python:

    x += 1
    x

    But I don't know how to combine an
    assignment and an expression into one
    expession. In JavaScript one can use

    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.

    Lawrence D'Oliveiro schrieb:
    On Thu, 07 Nov 2024 12:55:53 +0530, Annada Behera wrote:

    I heard this behavior is because python's integers are immutable.

    Nothing to do with that.

    ++x or x++ will redefine 5 to 6, which the interpreter forbids ...

    One of those is actually syntactically valid.

    It just won’t do what you expect it to do.


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From dn@PythonList@DancesWithMice.info to comp.lang.python on Sat Nov 9 08:09:49 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Mild Shock@janburse@fastmail.fm to comp.lang.python on Fri Nov 8 20:49:55 2024
    From Newsgroup: comp.lang.python

    Ok here you go, the "walrus operator" is
    actually a good lead, we have that this
    here from JavaScript:

    x++

    respectively

    ++x

    Can be replaced by the Python expression:

    (x := x + 1) - 1

    respectively

    (x := x + 1)

    Here is a test only testing x++:

    Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
    x = 5
    (x := x + 1) - 1
    5
    x
    6

    dn schrieb:
    ... irrational drivel removed ..> The 'walrus operator' could be applied:

    x = 5
    y = (x := x + 1); x
    6
    x, y
    (6, 6)... irrational drivel removed ..
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Thomas Passin@list1@tompassin.net to comp.lang.python on Fri Nov 8 17:00:31 2024
    From Newsgroup: comp.lang.python

    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)

    --- Synchronet 3.20a-Linux NewsLink 1.114