Python gotcha: loop variables pollute scope

February 28th, 2009

In Python, when you do this:

somelist = [somefunction(element) for element in somesequence]

the variable element lives on in the surrounding scope, contrary to what you might think.

If that scope is module scope (i.e. not inside a function or class definition), then you now have a global variable named element, which still carries the last value from somesequence.

Thus, anywhere else in the module (except the module scope prior to that loop), if you refer to a variable named element that you misspell or forget to declare in that local scope, then you are really referring to this global variable. Your helpful IDE will not highlight this, because it’s not strictly an error in the language sense. It’s just not what you intended.

(Actually, an IDE really should highlight this, certainly if it is being accessed somewhere other than module scope.)

The same goes for loop variables in generator expressions and for loops at module level, of course.

So, unless you’re writing a throwaway script or trivial code, just don’t do this. Instead, wrap the loop in a function:

def __initialize_somelist():
    return [somefunction(element) for element in somesequence]
somelist = __initialize_somelist()

Inspirational leadership

October 2nd, 2008


This great Nation will endure as it has endured, will revive and will prosper. So, first of all, let me assert my firm belief that the only thing we have to fear is fear itself — nameless, unreasoning, unjustified terror which paralyzes needed efforts to convert retreat into advance.

— Roosevelt on the Great Depression, first inaugural address, March 4, 1933

We shall go on to the end, we shall fight in France, we shall fight on the seas and oceans, we shall fight with growing confidence and growing strength in the air, we shall defend our Island, whatever the cost may be, we shall fight on the beaches, we shall fight on the landing grounds, we shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender

— Churchill on World War II, speech in the House of Commons, June 4, 1940

If money isn’t loosened up, this sucker could go down.

— George W. Bush on the economy, according to NYT, Sep 25, 2008

(It’s Jeff Matthews’ juxtaposition, I just spiffed up the references.)

(And okay, FDR and Churchill were making speeches and Bush was not. Still.)

A wee study in rhetorical tactics

September 9th, 2008

A random unrelated image

A random unrelated image

Respect for one’s opponent is a virtue most of us will acknowledge, but not all of us practice.

Clive Crook, in his column in the Financial Times, feels that the latter failing particularly afflicts Democrat-leaning commentators (or “liberals” as they are respectfully referred to in fair and balanced U.S. media).

Whatever the truth or falsity of that, I am using his article as a case study in rhetorical tactics. I have no credentials toward such a study; these are the casual observations of a layman.

So, here’s how to write an article designed to sway public opinion against an opponent. In the quotations, the bold-face emphasis is my doing (and the bald-faced original is Crook’s, of course).

First, project some appreciation of the opponent’s scant virtues, in order to boost the credibility of the attacks that are to follow:

Democrats speak up for the less prosperous; they have well-intentioned policies to help them; they are disturbed by inequality, and want to do something about it. Their concern is real and admirable.

Then levy a related blanket accusation against the opponent’s mindset that is sufficiently subjective to (a) stick easily without clear proof, and (b) lend itself poorly to clear disproof:

Their sympathy comes mixed with disdain, and even contempt.

Note an important feature of this accusation: it is closely tied with the initial praise, so as to neutralize it.

Next, develop that accusation further, taking care to appeal to the innate human frailties of resentment and defensiveness about suggested inferiority:

Democrats regard their policies as self-evidently in the interests of the US working and middle classes. Yet those wide segments of US society keep helping to elect Republican presidents. How is one to account for this? Are those people idiots? Frankly, yes – or so many liberals are driven to conclude. Either that or bigots, clinging to guns, God and white supremacy; or else pathetic dupes, ever at the disposal of Republican strategists. If they only had the brains to vote in their interests, Democrats think, the party would never be out of power. But again and again, the Republicans tell their lies, and those stupid damned voters buy it.

Feel free to lay it on thick here, whipping up the reader’s ire. It will not be directed at you.

Bring up the familiar “liberal media bias”, assuring the audience that said media also look down on them:

It is an attitude that a good part of the US media share. The country has conservative media (Fox News, talk radio) as well as liberal media (most of the rest). Curiously, whereas the conservative media know they are conservative, much of the liberal media believe themselves to be neutral.

Play the elitism theme some more, rub it in:

Their constant support for Democratic views has nothing to do with bias, in their minds, but reflects the fact that Democrats just happen to be right about everything. The result is the same: for much of the media, the fact that Republicans keep winning can only be due to the backwardness of much of the country.

Portray the opponent’s supporters with reckless exaggeration (go wild and cartoonlike here, nobody is going to demand any verisimilitude):

[Sarah Palin] represents a great slice of the country that the Democrats say they care about – yet her selection induced an apoplectic fit.

Allude to misogyny if at all possible. Ideally do so in an unverifiable and undisprovable way:

For days, the derision poured down from Democratic party talking heads and much of the media too. The idea that “this woman” might be vice-president or even president was literally incomprehensible.

Mine the most damning reactions among the opponent’s public supporters — even if you have to go to the edgy comedians, that works just fine — and depict them as being typical:

The popular liberal comedian Bill Maher, whose act is an endless sneer at the Republican party, noted that John McCain’s case for the presidency was that only he was capable of standing between the US and its enemies, but that should he die he had chosen “this stewardess” to take over.

Then deftly project this worst sneering you can find, onto the rest of his supporters, and by implication, your opponent himself. This is a combination of “they’re all the same” with a classic “let them deny it” manoeuvre:

I give Mr Maher credit for daring to say what many Democrats would only insinuate.

But here you have left yourself open to a very valid retort: Maher’s scorn was obviously no less targeted at the VP candidate’s complete and utter lack of experience in foreign affairs and defense, than at her refreshingly non-septuagenarian appearance. To parry that, preemptively deflect the point by half-conceding it — and immediately deliver an elitism riposte:

This joke was not – or not only – a complaint about lack of experience. It was also an expression of class disgust.

Do not get carried away with concessions, though — for example, certainly do not concede that the opposing side’s brisk reactions to Palin might have had to do with to-be-expected Democrat concerns such as the VP candidate supporting creationism in schools; being a hardliner on abortion; displaying an interest in banning books; and referring to the Iraq war as hopefully “a task that is from God”. Carefully portray all opposition as mere sexism and class snobbery.

There is still some risk that your target audience is not sufficiently aware of the disdain you claim they are constantly shown. So remind them again:

Voters in small towns and suburbs, forever mocked and condescended to by metropolitan liberals, are attuned to this disdain.

Sneak in a not-so-subtle suggestion as to what they should do about it, but do not let it sound as if you want to encourage them to do that — you are just casually making a general observation:

Every four years, many take their revenge.

But now, ease up a little. As in the beginning, enhance your credibility as a fair and balanced commentator by ending your article with a charm offensive. First, praise the opponent a bit. Do not make it phony. If possible, say something you might mean:

The irony in 2008 is that the Democratic candidate, despite Republican claims to the contrary, is not an elitist. Barack Obama is an intellectual, but he remembers his history. He can and does connect with ordinary people. His courteous reaction to the Palin nomination was telling.

Still, jab a little, but make sure to sound very nice about it:

Mr Obama made a bad mistake when he talked about clinging to God and guns, but I am inclined to make allowances

To further deflect accusations of bias, imply that you genuinely wish the opposing side would overcome the weaknesses you have described:

If only the Democrats could contain their sense of entitlement to govern in a rational world, and their consequent distaste for wide swathes of the US electorate, they might gain…

Still, lace that with another smidgeon of the air-of-superiority poison:

… the unshakeable grip on power they feel they deserve.

As a final coup de grâce, blithely slap your opponents with the commie AND the terrorist labels in one go. That’s simple. Just portray them as being against … wait for it …

They will have to develop some regard for the values that the middle of the country expresses when it votes Republican. Religion. Unembarrassed flag-waving patriotism. Freedom to succeed or fail through one’s own efforts.

Yes, go ahead, use the “They Hate Our Freedom” line.

Now that is rhetoric.

Of course, throughout this process of decrying the disrespect of the opposing side, leave unspoken your implicit suggestion that your own side’s talking heads represent — by contrast — the very epitome of respectful discourse. They certainly haven’t been outed for equally contemptuous commentary, along with a generous helping of double standards. They are no liberals, after all.

Note: I took this article as an example because (a) I got the idea reading this article, and (b) I’m not too fond of the McCain-Palin ticket. (Could you tell?) I don’t doubt that a similar set of tactics pervades Democrat-leaning text — just like the article author presumably doesn’t doubt that disrespect and contempt are shown on his side as well. Excuse us both for exercising our bias just a tad.

In context

February 14th, 2008

Two news items side by side on CNN Money:

CNN juxtaposition

Ano-novo

December 31st, 2007

Flugeldar

Fugato.net wishes its tireless, deluged readers a happy new year with thanks for everything, online and off, in the years gone by.

Winter solstice

December 24th, 2007

Winter solstice on the Tyrrhenian Sea

Vífill Atlason and sourpusses

December 14th, 2007

Vífill Atlason (16) made the news last weekend for prank-calling a supposedly top-secret number at the White House, pretending to be the Icelandic president, and booking a phone appointment with President Bush.

Ha ha, but the US news outlets missed the good part: the followup. Two TV stations ran interviews with Vífill. He showed up for one, and had a very-different-looking friend do the other, pretending to be him.

The duped station made it funnier still by threatening to sue both Vífill and the other TV station (which seems also to have been pranked, in that Vífill had promised to appear only on that station that day. Technically, of course, he kept his promise.) The reporter tendered his resignation, which his boss did not accept.

The same station has a show called Tekinn (“Taken” or “Duped”), running pranks on people with hidden cameras.

Vífill may in all seriousness be denied entrance to the US in the future for this (and possibly shackled and denied food and drink for 14 hours), the Dept. of Homeland Security being the celebrated Orwellian juggernaut of common sense and discretion that it is.

But he’ll have no trouble getting into Icelandic showbiz. (Except perhaps on Stöð 2.)

Reuters and the postal system

October 9th, 2007

My office address is in 101 Reykjavík, Iceland. We just received a letter from Reuters, addressed thus:

Laekjargata 4
200 Kopavogur
Norway

Pretty good work by the postal system! And as for Reuters, the dependable, accurate information source, well …

T-wit

September 29th, 2007

On non-bushy thinking

September 29th, 2007

Sagan and the solar system

I’m often asked the question, “Do you think there is extraterrestrial intelligence?” I give the standard arguments — there are a lot of places out there, and use the word billions, and so on. And then I say it would be astonishing to me if there weren’t extraterrestrial intelligence, but of course there is as yet no compelling evidence for it. And then I’m asked, “Yeah, but what do you really think?” I say, “I just told you what I really think.” “Yeah, but what’s your gut feeling?” But I try not to think with my gut. Really, it’s okay to reserve judgment until the evidence is in.

— Carl Sagan, The Burden of Skepticism, in The Skeptical Inquirer, Vol. 12, Fall 1987