[Edu-sig] Using try / except: any stipulations against routine use?

kirby urner kirby.urner at gmail.com
Thu Dec 15 19:41:42 CET 2011


On Thu, Dec 15, 2011 at 10:18 AM, Christian Mascher
<christian.mascher at gmx.de> wrote:
>
>>> On the other hand,
>>>     res_dict[ext] = res_dict.get(ext, 0) + 1
>>>
>> Isn't this at least as readable, and conceptually simpler?
>>
>>    if ext in res_dict:
>>        res_dict[ext] += 1
>>    else:         res_dict[ext] = 1
>>
>
>
> I haven't done much coding in Python lately. And I found this much more
> readable than the .get method which I forgot about. I positively didn't
> understand the oneliner just by reading it over and over again.
>

I might try something like this to make it clearer.

I can cut and paste such code snippets from my Replies
archive (another way to save time -- I edit from a pool
and splice in what's custom).

This goes off the common cartoon convention of a
prisoner putting one stroke on a wall for each day
in prison.  Here we use a string of "bars" (apropos).

>>> def strokes(prisoner, days=1):
	cells[prisoner] = cells.get( prisoner, "") + "|"*days

	
>>> cells = {}
>>> strokes("Mandela")
>>> cells
{'Mandela': '|'}
>>> strokes("Aung San Suu Kyi",10)
>>> cells
{'Aung San Suu Kyi': '||||||||||', 'Mandela': '|'}
>>> strokes("Mandela")
>>> strokes("Mandela")
>>> cells
{'Aung San Suu Kyi': '||||||||||', 'Mandela': '|||'}

What's also important is cells is global and need not be declared
global in strokes for this to work.  That's a stumbling block in
Lesson 12 or one of those, I forget which exactly, and is a
subtle aspect of Python:  you can poke items into a global
dict from a more local scope, just not rebind the name to some
other object.

> Of course I could easily have opened idle and looked up the .get method
> online (or looked in a manual).
>

Our you could wake up in the morning and have
a bunch of fresh feedback from a faithful mentor,
who points stuff out that passes for "accepted
holes" out there, but you could use to your advantage
in a job interview.  You know about dict.get, wow.
Hey, it's a competitive market out there.

I think we should have a whole thread on defaultdict
though.  I think we should also expose students to
importing django not to run a web service at first,
but to gain access to some low-level goodies like the
ordered dict.  Treat zope the same way -- just
something to import and "make math" with (interactive
console in CR or Eclipse in the current working
versions).

> The really good thing about Python is that so much of the core language
> doesn't surprise you. One is able to write code which is understandable even
> to people with very little experience or to yourself after not coding in
> Python for years. Nobody has to write such clear-cut code all the time, but
> at least it is possible with very little effort.
>

Yes, which is why Python is favored by the scientific
community.  It's powerful, thanks to libraries like numpy
and visual, comes with pyMol and sciPy and like that,
and you don't have to turn yourself into some compsci
geek to use it, just hack away and it makes sense, does
what you expect, fits your brain.

Python is great because now the physics team doesn't
have to sit around waiting for some back office "programming
team" to do some slow-poking away off camera.  "No, we
can do it ourselves!" -- the sense Python inspires.

I'm all for this.  As a long time math reformer, I've taken
the line that forcing Texas Instruments calculators down
the throats of K-12ers while denying them access to
free open source options such as Python, is malpractice
in the first degree and all student loans should be forgiven,
based on what was done to you people, and how you're
unemployable now, living in a tent someplace.  Dang.
Wouldn't it have been nice if the teachers had listened.

Kirby


> Christian
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig


More information about the Edu-sig mailing list