[Edu-sig] Interesting "gotcha"

kirby urner kirby.urner at gmail.com
Tue Mar 29 18:12:50 CEST 2011


On Mon, Mar 28, 2011 at 10:06 PM, Michael H. Goldwasser <goldwamh at slu.edu>wrote:

>
> To start a new thread, I'm always trying to keep a list of some common
> "gotchas" that beginning students run across when using Python, or
> things that teachers should keep in mind when teaching with the
> language.


Sincere applause -- exactly the kind of thread edu-sig should include
in the weave.

This mystr.isupper( ) == True:

stuff is quite common.  One also gets veterans of other languages
wanting to use while instead for for loops:

cnt = 0
while cnt < len(mylist):
    theword = mylist[cnt]
    of theword.isupper( ) == True:
        is_upper.append(theword)
    cnt += 1

instead of

is_upper = [ theword for theword in mylist if theword.isupper() ]

I was at one of David Goodger's talks which was all about
getting more idiomatic.

Knowing the default behavior of dicts means you know you
can say

for key in thedict:

instead of

for key in thedict.keys():

Sometimes I'll think a student is using an idiom that looks odd
and so isn't "Pythonic" (as if I were the only judge). Like:

mydict.update({key:value})

instead of

mydict[key] = value

but then I'll do a search and see enough authors using the idiom
to update my own ideas of what's idiomatic.

Teachers have a privileged position, getting to see the
same solutions over and over but in slightly (or greatly)
different styles.

edu-sig makes a useful repository for those wishing to
share from this perspective.

Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20110329/15b2b279/attachment.html>


More information about the Edu-sig mailing list