Trigraph Idiom - Original?

John Machin sjmachin at lexicon.net
Wed Apr 27 20:30:48 EDT 2005


On 27 Apr 2005 16:26:02 -0700, "Jeff  Winkler" <winkler1 at gmail.com>
wrote:

>I've come up with a trigraph idiom, am curious if it's been done before
>(probably). I like to use trigraphs occasionally.
>
>Scenario: I'm doing an RSS->Javascript conversion for our intranet. I'd
>like to use different CSS class if a post is new. Code:
>
>hoursOld=abs(time.time()-time.mktime(i.modified_parsed))/3600
>cssClass=['rssLink','rssLinkNew'][hoursOld<12]
>entry='<a href="%s" class="%s" target="detail">%s</a>' %
>(cssClass,i['link'],i['title'])
>
>So, ['rssLink','rssLinkNew'] is index by boolean value- False:0, or
>True:1.
>
>I realize many will find this hideous, but 3 lines of code to do
>something simple seems equally bad. Thoughts? Is there a better way?

1. You appear to be conflating "trigraph" [a truly hideous
little-known lexical feature of C] and "ternary operator".

2. No, it's not original; the idea of using a boolean value as index
to an array of 2 elements probably occurred to somebody at about the
time that subroutines were invented. Look at the threads that break
out periodically in this newsgroup: "why doesn't python have a ternary
operator", "why doesn't 'foo and bar or zot' work sometimes" etc etc
-- it usually gets a mention.

3. Some might say it is hideous, but perhaps less hideous than (a)
using "i" as a reference to data other than a loop index (b) being so
worried about the longevity of your keyboard and/or thumb(s) that you
hit the spacebar only once or twice (other than inside string
literals) in three statements.

4. In the 3rd statement, you appear at best to have gravely misleading
variable names -- cssClass is shoved into the href="%s" but i['link']
is shoved into the class="%s" -- or worse, a stuffup.






More information about the Python-list mailing list