Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196986

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

From Thomas Passin <list1@tompassin.net>
Newsgroups comp.lang.python
Subject Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)
Date 2024-11-08 17:00 -0500
Message-ID <mailman.89.1731115813.4695.python-list@python.org> (permalink)
References (2 earlier) <050c2ce9efd8442fb902ecc926afb1ee42fe6c34.camel@tilde.green> <vgjoe9$2stii$2@dont-email.me> <vgjq77$aj8c$1@solani.org> <59a78893-d96a-4f5b-921f-5ad862449ca2@DancesWithMice.info> <5b6c9d39-2a1a-4554-8cd8-da123411eb81@tompassin.net>

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-06 17:27 +0100
  Re: Two aces up Python's sleeve Annada Behera <annada@tilde.green> - 2024-11-07 12:55 +0530
    Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-07 15:04 +0100
      Re: Two aces up Python's sleeve Greg Ewing <greg.ewing@canterbury.ac.nz> - 2024-11-08 11:15 +1300
        Re: Two aces up Python's sleeve dn <PythonList@DancesWithMice.info> - 2024-11-08 13:07 +1300
        Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-08 01:25 +0100
          Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-08 01:29 +0100
            Re: Two aces up Python's sleeve Mild Shock <janburse@fastmail.fm> - 2024-11-08 01:47 +0100
    Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-11-08 01:10 +0000
      Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Mild Shock <janburse@fastmail.fm> - 2024-11-08 02:40 +0100
        Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) dn <PythonList@DancesWithMice.info> - 2024-11-09 08:09 +1300
          Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Mild Shock <janburse@fastmail.fm> - 2024-11-08 20:49 +0100
        Re: Two aces up Python's sleeve (Posting On Python-List Prohibited) Thomas Passin <list1@tompassin.net> - 2024-11-08 17:00 -0500

csiph-web