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

the fucking new and init

http://jaredgrubb.blogspot.com/2008/01/pythons-new-and-init.html

getattr. getattr, setattr

getattr. __getattr__, __setattr__

descriptor

descriptor

metaclass

design patern

singleton

http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init

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

http://timka.org/tech/2008/12/17/singleton-in-python/

decorator

step 1) understand what is closure. http://www.shutupandship.com/2012/01/python-closures-explained.html
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. http://ynniv.com/blog/2007/08/closures-in-python.html
step 3) better understand closure and basic decorator. http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
step 4) three kinds of decorator

http://www.artima.com/weblogs/viewpost.jsp?thread=240808

overriding methods and variables in subclasses

instance variables

http://antroy.blogspot.com/2007/04/shadowing-and-overriding-in-java-and.html

# In A.py
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)
3

  1. This variable is shared by all classes.

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

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

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

  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
10
»> classinstance2.common
30
»> MyClass.common = 50

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

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

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")
hello
»> classinstance.myfunction(1, 2)
3

  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
10

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

http://mrjoes.github.io/2013/06/21/python-realtime.html

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