Hexa's Blog

Python - learning

07/12/2014 cheatsheet

This is a remember note to learn python, more or less, it’s writen for me -the author to rememeber main issues of python.

1. Scope and Namespace

def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    print("After local assignment:", spam)
    print("After nonlocal assignment:", spam)
    print("After global assignment:", spam)
print("In global scope:", spam)

The output is:

After local assignment: test spam  
After nonlocal assignment: nonlocal spam  
After global assignment: nonlocal spam  
In global scope: global spam


  1. Local variable always is always used inside local scope. In the example, local variable spam has value is local spam, when do_local() invoked, spam only created inside a function, it has no use outside.
  2. Non local variable has been declared and it affect within scope_test and only within scope_test.
  3. Global variable only used in global scope.
  4. In Python, a function can exist inside a function, in the example above, do_local insides scope_test.
  5. Functions make its own scope.

##2. Class

class Dog:

    kind = 'canine'         # class variable shared by all instances
    def __init__(self, name):
        self.name = name    # instance variable unique to each instance
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # shared by all dogs
>>> e.kind                  # shared by all dogs
>>> d.name                  # unique to d
>>> e.name                  # unique to e


  1. Class variables shared by all instances, kind is class variable.
  2. Instance variables shared by each instance,name is instance variable.
  3. Function can be define outside class.
# Function defined outside the class
def f1(self, x, y):
    return min(x, x+y)

class C:
    f = f1
    def g(self):
        return 'hello world'
    h = g

3. Inheritence

class DerivedClassName(BaseClassName):

class DerivedClassName(modname.BaseClassName):

Call baseclass method, base class must be in global scope:

BaseClassName.methodname(self, arguments)

Check instance type:

isinstance(obj, int) #check if `obj` is instance of `int`
issubclass(bool, int) #check if `bool` is subclass of `int`