- Dive into Python
- python magic methods
- python gotchas
- Tips
- Cheatsheet
- http://rgruet.free.fr/PQR26/PQR2.6.html
- http://michaelgoerz.net/refcards/python_refcard.pdf
- http://www.scottklarr.com/topic/105/python-cheat-sheets/#comments
- http://www.neurosoftware.ro/wp/elvis/cheatsheets/cheat-sheet-ajax-css-php-xml-mysql-javascript-ruby-latex-%E2%80%A6/
- http://www.cogsci.rpi.edu/~destem/gamedev/python.pdf
the fucking new and init
http://jaredgrubb.blogspot.com/2008/01/pythons-new-and-init.html
getattr. getattr, setattr
getattr. __getattr__, __setattr__
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
- This variable is shared by all classes.
»> classinstance2 = MyClass()
»> classinstance.common
10
»> classinstance2.common
10
- Note how we use the class name
- instead of the instance.
»> MyClass.common = 30
»> classinstance.common
30
»> classinstance2.common
30
- This will not update the variable on the class,
- instead it will bind a new object to the old
- variable name.
»> classinstance.common = 10
»> classinstance.common
10
»> classinstance2.common
30
»> MyClass.common = 50
- This has not changed, because "common" is
- now an instance variable.
»> classinstance.common
10
»> classinstance2.common
50class 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
- This class doesn't have a .test member, but
- we can add one to the instance anyway. Note
- 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