- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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
Admin
... 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.
Admin
I'm with you on this. It's a long-standing idiom for constructing a C-like ternary in Python, using
and
instead of?
andor
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.Admin
I mean, this is exactly how you would do it in a shell script, so it's hardly utterly bizarre. Albeit non-pythonic.
Admin
To me, this feels like a "know your tools" situation. In Javascript, things like that are common practice.
Admin
_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?Admin
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
Admin
If this was Perl, I could have written that code. For the same reasons, plus job protection.
Admin
A function named "_b"? This is Ninja Code
https://javascript.info/ninja-code
Admin
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.
Admin
“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
Admin
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.
Admin
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 thematch/case
statement, which can be used similarly but also implements sophisticated pattern matching.Admin
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.