Idioms and Anti-Idioms in Python

Idioms and Anti-Idioms, AKA Do and Don't


Prue (Something Wicca This Way Comes, season 1) -- No, we are not supposed to use our powers

Python


Prue (Something Wicca This Way Comes, season 1) -- Uh, it doesn't work out there either.

Exceptions


Leo (Paige From the Past, season 4) -- You have to [...] Paige. No exceptions.

Exceptions -- Catching Too Much


Piper (Charmed Again, season 4) -- Okay, well this is way too much for me to handle.

Exceptions -- Catching Too Much -- Example

try:
   f = opne("file")
except:
   sys.exit("no such file")

Piper (Charmed Again, season 4) -- Way too much.

Exceptions -- Catching Too Soon


Phoebe (Knight to Remember, season 4) -- Maybe it's just too soon.

Exceptions -- Catching Too Soon -- Example

def readlinesfromfile(file):
    try:
        return open(file).readlines()
    except IOError:
        pass # do what?

Paige (Knight to Remember, season 4) -- I'm already a little late.

Catching multiple exceptions

try:
   fp = open("file")
except IOError, OSError:
   print "could not open file"

Paige (Knight to Remember, season 4) -- Because I've got too many responsibilities

Catching multiple exceptions (cont'd)


Piper (Knight to Remember, season 4) -- Alright! Calm down!

Catching multiple exceptions (cont'd 2)

try:
   fp = open("file")
except (IOError, OSError):
   print "could not open file"

Phoebe (Knight to Remember, season 4) -- Besides that, maybe we can help

Catching NameError

try:
   import foo
except ImportError:
   pass

try:
   foo.Function()
except NameError:
   pass # some replacement

Piper (Morality Bites, season 4) -- That's OK, I forgot your name too.

Catching NameError (cont'd)

try:
   import foo
except ImportError:
   foo = None

if foo is not None:
   foo.Function()
else:
   pass # some replacement

Anne (Morality Bites, season 4) -- Oh, right, sorry.

Importing Modules -- A Review


Phoebe (Animal Pragmatism, season 2) -- Rome was not built in a day,

Importing __main__


Piper (Animal Pragmatism, season 2) -- And why mess with a good thing?

Importing a File Twice

# file: hello.py
import hello
class Foo: pass

Phoebe (Which Prue Is It, Anyway?, season 1) -- Okay, which one of you is the real Prue?

Importing *

from os import *

fp = open("file") # works
fp.readline() # fails with a weird error...?

Pink Prue (Which Prue Is It, Anyway?, season 1) -- So, um, what did I do now?

Importing * Inside Functions


Pink Prue (Which Prue Is It, Anyway?, season 1) -- What ever it is, I have an alibi.

Importing Names


Real Prue (Which Prue Is It, Anyway?, season 1) -- Because I still have to work here when all of this is over.

Reloading


Real Prue (Which Prue Is It, Anyway?, season 1) -- Don't worry I'm never casting that spell again.

exec, execfile and eval


Reporter (Morality Bites, season 2) -- More news on the execution of Phoebe Halliwell coming up.

exec, execfile and eval -- Modify namespaces


Nathaniel (Morality Bites, season 2) -- Executions are a bitch to plan.

exec, execfile and eval -- Inside functions


Nathaniel (Morality Bites, season 2) -- Phoebe, what is this? An attempt to stay your execution?

Conclusion: recommended usage


Phoebe (Morality Bites, season 2) -- Just because you don't understand something, doesn't make it evil.

exec, execfile and eval -- Restricted Execution (Don't)


Leo (Morality Bites, season 2) -- Nobody's gonna rescue you.

Syntax


Prue (Morality Bites, season 2) -- You know, we can still make the good things happen.

Syntax -- Tabs and Spaces


Prue (The Painted World, season 2) -- We've seen so many bizarre things.

Syntax -- Backslash Continuations

# Extra newline
r = 1 \

+2

# Missing backslash in long series
r = 1 \
+2 \
+3 \
+4 
+5 \
+6

Syntax -- Backslash Continuations (cont'd)

# Extra newline
r = (1

+2)

# Long series
r = (1
+2
+3
+4
+5
+6)

Prue (The Painted World, season 2) -- Uh, what just happened here?

Hand Hacking Batteries


Prue (Animal Pragmatism, season 2) -- Well, we didn't find anything in the Book Of Shadows.

Further Reading


Phoebe (The Painted World, season 2) -- I think you'll find me pretty knowledgeable about all areas

Questions?

Piper (The Painted World, season 2) -- You're like ask rainman.com

Bonus Slides

Phoebe (The Painted World, season 2) -- Oh, and P.S. there will be no personal gain.

Packages and __init__.py


Piper (Animal Pragmatism, season 2) -- It's a package. One I would like to share with you.

Type Checking


Phoebe (Black as Cole, season 2) -- I never thought of myself as the marrying type

Type Checking -- Example

class Foo:
    def __init__(self, i):
        if type(i) is types.StringType:
            self.content = open(i).readlines()
        elif type(i) is types.ListType:
            self.content = i

Phoebe (Muse to My Ears, season 4) -- You're an artistic, creative type.

Type Checking -- Example -- Fixed


class Foo:
    pass

class FooFromFile(Foo):

    def __init__(self, filename):
         self.content = open(filename).readlines()

class FooFromList(Foo):

    def __init__(self, list):
         self.content = list

Phoebe (Muse to My Ears, season 4) -- You see how well this worked out?

Private __Attributes


Tessa (Animal Pragmatism, season 2) -- Maybe it's our fault because we tried to make them into something they're not.

Using Mutable Default Arguments

def foo(l=[]):
    l.append(5);return l
def foo(l=None):
    if l is None: l=[]
    l.append(5);return l

Snake guy (Animal Pragmatism, season 2) -- You two are acting like nothing's changed.