• mado (unregistered)

    Actually, this was a common pattern back in the days when Python didn't have "A if condition else B" yet. I first read about it in https://linux.die.net/diveintopython/html/power_of_introspection/and_or.html#d0e9975

  • I'm not a robot (unregistered)

    ... You do know this was the usual way of doing a conditional expression before the "... if ... else ..." syntax was added, right? It might worth a slight slap on the wrist for not keeping up (and even then it depends on how old the code is, and whether there was a reasonable expectation at the time of being compatible with 2.4 and below), but it's not like they just pulled out it of their nether regions.

  • (nodebb) in reply to I'm not a robot

    I'm with you on this. It's a long-standing idiom for constructing a C-like ternary in Python, using and instead of ? and or instead of :. Um. And making sure that the value for the "true" case isn't falsy.

    And the ... if ... else ... syntax is also a ternary, but one that has the oddity that the condition is in the middle.

  • David-T (unregistered)

    I mean, this is exactly how you would do it in a shell script, so it's hardly utterly bizarre. Albeit non-pythonic.

  • [ ] I'm not a robot. (unregistered)

    To me, this feels like a "know your tools" situation. In Javascript, things like that are common practice.

  • (nodebb)

    _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) - wouldn't that get us into trouble when x is a null reference?

  • IPGuru (unregistered)

    As others have stated this was std practice before the a = b if Cond else c construct was introduced.

    it is also worth noting that Guido fought against implementing it as unnecessary for many years until he was bitten by an edge case that caused the And Or pattern to fail.

    The appears to be an example of a programmer failing to keep up with new features

  • Lupe (unregistered)

    If this was Perl, I could have written that code. For the same reasons, plus job protection.

  • Sauron (unregistered)

    A function named "_b"? This is Ninja Code

    https://javascript.info/ninja-code

  • adhdeveloper (unregistered)

    Many are pointing out that this was considered standard practice in Python for a long time.

    I'll then ask the question, if you need to do something as convoluted as this doesn't that mean the language is a bit of a WTF.

    Also why is Python so full of idioms? It seems that many things I want to do in it requires an "idiom" by the term idiom we mean "code that is non obvious and doesn't make intuitive sense unless you've memorized a certain pattern of code meaning a certain higher level operation."

    In conclusion you can argue this is normal and acceptable and I can argue you've developed Stockholm Syndrome.

  • (nodebb)

    “Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” ― Brian Kernighan

  • MaxiTB (unregistered) in reply to Sauron

    I'm pretty sure your blog only applies to JavaScript.

    In C# it's pretty common to declare member fields camel case with leading underscore (and that makes totally sense to avoid a ton of this boilerplate code or confusingly named local variables).

    So I wouldn't be surprised if other languages have similar styling rules - then again, C# is pretty unique with naming rules being defined from the start. Java as another extreme example is a literal mess when it comes to naming.

  • Barry Margolin (github) in reply to adhdeveloper

    But it's NOT needed any more. The pattern became common because Python didn't have a ternary operator, so you could say that Python was a WTF at the time. But then the problem was remedied by adding the ternary. This was in Python 2.5, which was released in 2006.

    This is how programming languages evolve -- if you notice idioms popping up to fill a hole, and the idiom seems ugly, you add a new feature with nicer syntax.

    In some areas Python has been slow to change. Most languages have a switch/case statement, but Python resisted it until they designed the match/case statement, which can be used similarly but also implements sophisticated pattern matching.

  • osmarks (unregistered)
    Comment held for moderation.
  • pythonista (unregistered)

    TRWTF is that the Py2/3 code isn't needed at all and is even worse because of it.

    In Python 2 when you encode a string/unicode string, you get an encoded string (which can be treated exactly like bytes). In Python 3 you get bytes.

    The name _b alludes to this. I would guess the function is a "to_bytes" function.

    If you want bytes, call encode, always. This code is also subtly wrong in that if you pass it a non-latin1-encoded string in Python 2, you'll silently get a string with the wrong encoding.

  • Chris (unregistered)
    Comment held for moderation.
  • löchlein deluxe (unregistered)
    Comment held for moderation.
  • Jeremy (unregistered)
    Comment held for moderation.
  • Arthur (unregistered)
    Comment held for moderation.
  • JC (unregistered)
    Comment held for moderation.
  • CADD Center (unregistered)
    Comment held for moderation.
  • www.jayavintravels (unregistered)
    Comment held for moderation.
  • bridalmakeupartistvijay (unregistered)
    Comment held for moderation.
  • Forerun (unregistered)
    Comment held for moderation.
  • Ninos (unregistered)
    Comment held for moderation.
  • krishnanborewells (unregistered)
    Comment held for moderation.
  • RussellF (unregistered)
    Comment held for moderation.

Leave a comment on “Clever And Or Not”

Log In or post as a guest

Replying to comment #:

« Return to Article