Cook Book
  1. Dive into Python
  2. python magic methods
  3. python gotchas
  4. Tips
  5. Cheatsheet

the fucking new and init

getattr. getattr, setattr

getattr. __getattr__, __setattr__




design patern


def singleton(cls):
    instances = {}
    def getinstance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance
class MyClass:


step 1) understand what is closure.
step 2) classInstance.method is also a closure, which stores the value of classInstance and pass that as the 1st argument of method, ie self. as a closure , it close on classInstance.
step 3) better understand closure and basic decorator.
step 4) three kinds of decorator

overriding methods and variables in subclasses

instance variables

# In
class A(object):
  def __init__(self):
      self.x = "In class A"
  def getX(self):
      return self.x
class B(A): 
  def __init__(self): 
    self.x = "In class B"
b = B()
print b.getX()

Running this example will print "In class B". because Python make sure the same variable name is overwritten.

class variables

class MyClass(object):
common = 10
def init(self):
self.myvariable = 3
def myfunction(self, arg1, arg2):
return self.myvariable

# This is the class instantiation
»> classinstance = MyClass()
»> classinstance.myfunction(1, 2)

  1. This variable is shared by all classes.

»> classinstance2 = MyClass()
»> classinstance.common
»> classinstance2.common

  1. Note how we use the class name
  2. instead of the instance.

»> MyClass.common = 30
»> classinstance.common
»> classinstance2.common

  1. This will not update the variable on the class,
  2. instead it will bind a new object to the old
  3. variable name.

»> classinstance.common = 10
»> classinstance.common
»> classinstance2.common
»> MyClass.common = 50

  1. This has not changed, because "common" is
  2. now an instance variable.

»> classinstance.common
»> classinstance2.common

class OtherClass(MyClass):
# The "self" argument is passed automatically
# and refers to the class instance, so you can set
# instance variables as above, but from inside the class.
def init(self, arg1):
self.myvariable = 3
print arg1

»> classinstance = OtherClass("hello")
»> classinstance.myfunction(1, 2)

  1. This class doesn't have a .test member, but
  2. we can add one to the instance anyway. Note
  3. that this will only be a member of classinstance.

»> classinstance.test = 10
»> classinstance.test

global variable

Global variables are declared outside of functions and can be read without any special declarations, but if you want to write to them you must declare them at the beginning of the function with the "global" keyword, otherwise Python will bind that object to a new local variable (be careful of that, it's a small catch that can get you if you don't know it). For example:

number = 5
 def myfunc():
     # This will print 5.
     print number
 def anotherfunc():
     # This raises an exception because the variable has not
     # been bound before printing. Python knows that it an
     # object will be bound to it later and creates a new, local
     # object instead of accessing the global one.
     print number
     number = 3
 def yetanotherfunc():
     global number
     # This will correctly change the global.
     number = 3

python and real-time

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License