Re: Ifs and assignments

Discussion in 'Python' started by Terry Reedy, Jan 3, 2014.

  1. Terry Reedy

    Terry Reedy Guest

    On 1/2/2014 8:20 PM, Mark Lawrence wrote:
    > On 03/01/2014 00:57, Gary Herron wrote:
    >> On 01/02/2014 01:44 PM, John Allsup wrote:
    >>> The point of my original post was that, whilst C's
    >>> if( x = 2 ) { do something }
    >>> and
    >>> if( x == 2 ) { do something }
    >>> are easy to confuse, and a source of bugs, having a construct like
    >>> follows:
    >>>
    >>> if x == 2:
    >>> do something # what happens at present
    >>> if testFunc() as x:
    >>> do something with x
    >>>
    >>> using the 'as' syntax that appears with 'with' and 'except', would allow
    >>> for the advantages of C style assignments in conditionals but without
    >>> the easy confusion, since here the syntax is significantly different
    >>> between assignment and equality testing (rather than a character apart
    >>> as happens with C).
    >>>
    >>> This occurs further down in my original post (past the point where you
    >>> inserted your reply).
    >>>
    >>> Another post suggested a workaround by defining a 'pocket' class, for
    >>> which I am grateful.
    >>>
    >>> John

    >>
    >> Sorry. I shot off my answer before reading the whole post. That's
    >> never a good idea.
    >>
    >>
    >> After reading to the end, I rather like your suggestion. It works well
    >> in your example, , nicely avoids the C/C++ trap, and has some
    >> consistency with other parts of Python.
    >>
    >> Gary Herron
    >>
    >>

    >
    > I liked the look of this as well. It ought to go to python ideas, or
    > has it been suggested there in the past?


    Yes, and rejected (I am quite sure). Consistency would 'demand' at least
    "while expr as target" and possibly "for i in iterable_expr as target".


    --
    Terry Jan Reedy
     
    Terry Reedy, Jan 3, 2014
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Dehmann
    Replies:
    8
    Views:
    2,095
    bartek
    Jun 2, 2004
  2. charlie

    Nested ifs and speed.

    charlie, Jul 24, 2006, in forum: C Programming
    Replies:
    11
    Views:
    563
    BubbaGump
    Jul 26, 2006
  3. John Allsup

    Ifs and assignments

    John Allsup, Jan 2, 2014, in forum: Python
    Replies:
    0
    Views:
    91
    John Allsup
    Jan 2, 2014
  4. John Allsup

    Re: Ifs and assignments

    John Allsup, Jan 2, 2014, in forum: Python
    Replies:
    5
    Views:
    64
    Chris Angelico
    Jan 3, 2014
  5. Dennis Lee Bieber

    Re: Ifs and assignments

    Dennis Lee Bieber, Jan 3, 2014, in forum: Python
    Replies:
    8
    Views:
    73
    Mark Lawrence
    Jan 3, 2014
Loading...

Share This Page