Re: Ifs and assignments

Discussion in 'Python' started by Dennis Lee Bieber, Jan 3, 2014.

  1. On Thu, 02 Jan 2014 17:20:54 +0000, John Allsup <> declaimed
    the following:

    >Hi,
    >
    >This is my debut on this list.
    >
    >In many languages, such as C, one can use assignments in conditionals
    >and expressions. The most common, and useful case turns up when you


    Really? You can't do it in FORTRAN, BASIC, COBOL, Pascal, Ada...

    C, C++, Java, C# are all based on the same core syntax. For all
    effective purposes, they are the same as grouping M$ Visual BASIC,
    GW-BASIC, Kemeny&Kurtz TrueBASIC as "most languages, such as BASIC"...

    >have if/else if/else if/else constructs. Consider the following
    >non-working pseudoPython.
    >
    >import re
    >r1 = re.compile("hello (\d)")
    >r2 = re.compile("world([!?])")
    >
    >w = "hello world!"
    >

    matcher = [ (r1, handleMatch1), (r2, handleMatch2) ]

    >if m = r1.search(w):
    > handleMatch1(m)
    >elif m = r2.search(w):
    > handleMatch2(m)
    >else:
    > print("No match")
    >

    for r, h in matcher:
    m = r.search(w)
    if m:
    h(m)
    break
    else:
    print ("no match")

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Jan 3, 2014
    #1
    1. Advertising

  2. Dennis Lee Bieber wrote:

    > On Thu, 02 Jan 2014 17:20:54 +0000, John Allsup <>
    > declaimed the following:


    >>In many languages, such as C, one can use assignments in conditionals
    >>and expressions. The most common, and useful case turns up when you

    >
    > Really? You can't do it in FORTRAN, BASIC, COBOL, Pascal, Ada...
    >
    > C, C++, Java, C# are all based on the same core syntax. For all
    > effective purposes, they are the same as grouping M$ Visual BASIC,
    > GW-BASIC, Kemeny&Kurtz TrueBASIC as "most languages, such as BASIC"...



    Thank you for pointing this out! Far too many people imagine that C and the
    C-inspired languages make up the entire universe of programming.

    But having said that, features should be judged on their merit, not on
    whether they are familiar to Ada/Lisp/C/Algol/... programmers. (Although
    familiarity is one factor out of many which should be considered.)

    Personally, I find it hard to care about assignment as an expression. I find
    the obvious C-inspired syntax terrible, as it is too easy to mistakenly use
    == instead of = or visa versa:

    if m = re.match(pattern, text):
    ...

    Using "as" is slightly better:

    if re.match(pattern, text) as m:
    ...

    but I don't think it's sufficiently useful for me to get excited about it.

    However, I don't think we should treat this as specific to if statements:

    for i, obj in enumerate(alist + blist + clist as items):
    items = process(obj)


    Is that really better than this?

    items = alist + blist + clist
    for i, obj in enumerate(items):
    items = process(obj)



    --
    Steven
    Steven D'Aprano, Jan 3, 2014
    #2
    1. Advertising

  3. On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano
    <> wrote:
    > However, I don't think we should treat this as specific to if statements:
    >
    > for i, obj in enumerate(alist + blist + clist as items):
    > items = process(obj)
    >
    >
    > Is that really better than this?
    >
    > items = alist + blist + clist
    > for i, obj in enumerate(items):
    > items = process(obj)


    It definitely shouldn't be specific to if statements, though I think
    that's probably going to be the biggest use-case (chained if/elif
    blocks). Maybe a for loop isn't the best other example, but I
    frequently work with places where I want to call some function and
    keep iterating with the result of that until it returns false:

    while (var = func())
    {
    ....
    }

    In Python, that gets a lot clunkier. The most popular way is to turn
    it into an infinite loop:

    while True:
    var = func()
    if not var: break
    ....

    which is pretty much how the C version will compile down, most likely.
    It feels like an assembly language solution. The point of a while loop
    is to put its condition into the loop header - burying it inside the
    indented block (at the same indentation level as most of the code)
    conceals that, and this is a very simple and common condition. So this
    is what I'd cite as a second use-case:

    while func() as var:
    ....

    And that definitely looks more readable.

    ChrisA
    Chris Angelico, Jan 3, 2014
    #3
  4. On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano
    <> wrote:
    > Personally, I find it hard to care about assignment as an expression. I find
    > the obvious C-inspired syntax terrible, as it is too easy to mistakenly use
    > == instead of = or visa versa:


    Python has similar problems, though. It's inherent to the nature of
    symbolic languages.

    a = (1, 2, 3)
    b = {1, 2, 3}

    In many fonts, it's hard to tell one from the other without peering.
    Do people decry set literal notation in favour of explicitly writing
    the word "set"? No; and I think most of us agree that it's better to
    have the symbols. At least with == vs = there's a length difference. I
    don't think it's C's fault or problem that equality and assignment
    look similar and can be used in the same places, any more than it's a
    problem to have assignment and subtraction differ by only one stroke:

    a - (1, 2, 3)

    Is that confusingly similar to assignment?

    ChrisA
    Chris Angelico, Jan 3, 2014
    #4
  5. Dennis Lee Bieber

    Roy Smith Guest

    In article <>,
    Chris Angelico <> wrote:

    > On Fri, Jan 3, 2014 at 2:33 PM, Steven D'Aprano
    > <> wrote:
    > > Personally, I find it hard to care about assignment as an expression. I find
    > > the obvious C-inspired syntax terrible, as it is too easy to mistakenly use
    > > == instead of = or visa versa:

    >
    > Python has similar problems, though. It's inherent to the nature of
    > symbolic languages.
    >
    > a = (1, 2, 3)
    > b = {1, 2, 3}
    >
    > In many fonts, it's hard to tell one from the other without peering.
    > Do people decry set literal notation in favour of explicitly writing
    > the word "set"? No; and I think most of us agree that it's better to
    > have the symbols. At least with == vs = there's a length difference. I
    > don't think it's C's fault or problem that equality and assignment
    > look similar and can be used in the same places, any more than it's a
    > problem to have assignment and subtraction differ by only one stroke:
    >
    > a - (1, 2, 3)
    >
    > Is that confusingly similar to assignment?
    >
    > ChrisA


    I do this all the time:

    t0 = time.time()
    [some code]
    t1 = time.time()
    dt = t1 = t0 # <-- spot the typo?
    Roy Smith, Jan 3, 2014
    #5
  6. On Fri, Jan 3, 2014 at 3:00 PM, Roy Smith <> wrote:
    > I do this all the time:
    >
    > t0 = time.time()
    > [some code]
    > t1 = time.time()
    > dt = t1 = t0 # <-- spot the typo?


    Yep, I see that... now that it's pointed out as a typo. Without the
    marker, I'd assume it's correct chained assignment, and only an
    examination of the name (dt = delta t) hints otherwise.

    ChrisA
    Chris Angelico, Jan 3, 2014
    #6
  7. Dennis Lee Bieber

    Roy Smith Guest

    In article <>,
    Chris Angelico <> wrote:

    > while (var = func())
    > {
    > ....
    > }
    >
    > In Python, that gets a lot clunkier. The most popular way is to turn
    > it into an infinite loop:
    >
    > while True:
    > var = func()
    > if not var: break
    > ....


    Or turn it into a generator:

    def funcinator():
    while True:
    var = func()
    if var:
    yield var
    else:
    break

    for var in funcinator():
    ....

    That certainly makes your mainline code cleaner, but it's a lot of crud
    when all you really wanted to write was:

    while func() as var:
    ....
    Roy Smith, Jan 3, 2014
    #7
  8. On Fri, Jan 3, 2014 at 3:14 PM, Roy Smith <> wrote:
    > Or turn it into a generator:
    > That certainly makes your mainline code cleaner...


    Cleaner perhaps, but not clearer. Instead of seeing the original
    function (say, a database retrieve-next call), you see a wrapper and
    have to go dig to find out what it's actually doing. Unless it's some
    kind of generic handler, like:

    def funcinator(func,*args):
    while True:
    var = func(*args)
    if var:
    yield var
    else:
    break

    while funcinator(cur.getnext):
    ....

    But even that would be problematic until it got so thoroughly
    understood that it's like enumerate() - which is itself still not
    perfect.

    ChrisA
    Chris Angelico, Jan 3, 2014
    #8
  9. On 03/01/2014 04:25, Chris Angelico wrote:
    > On Fri, Jan 3, 2014 at 3:14 PM, Roy Smith <> wrote:
    >> Or turn it into a generator:
    >> That certainly makes your mainline code cleaner...

    >
    > Cleaner perhaps, but not clearer. Instead of seeing the original
    > function (say, a database retrieve-next call), you see a wrapper and
    > have to go dig to find out what it's actually doing. Unless it's some
    > kind of generic handler, like:
    >
    > def funcinator(func,*args):
    > while True:
    > var = func(*args)
    > if var:
    > yield var
    > else:
    > break
    >
    > while funcinator(cur.getnext):
    > ....
    >
    > But even that would be problematic until it got so thoroughly
    > understood that it's like enumerate() - which is itself still not
    > perfect.
    >
    > ChrisA
    >


    I find the above rather funcy :)

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Jan 3, 2014
    #9
    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,081
    bartek
    Jun 2, 2004
  2. charlie

    Nested ifs and speed.

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

    Ifs and assignments

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

    Re: Ifs and assignments

    John Allsup, Jan 2, 2014, in forum: Python
    Replies:
    5
    Views:
    59
    Chris Angelico
    Jan 3, 2014
  5. Terry Reedy

    Re: Ifs and assignments

    Terry Reedy, Jan 3, 2014, in forum: Python
    Replies:
    0
    Views:
    64
    Terry Reedy
    Jan 3, 2014
Loading...

Share This Page