Tuesday, October 11, 2011

Python study notes

python overview
  • abstraction: 
    • type: dynamic binding, strong type(for catching bugs)
    • object is first class:objects can be freely passed around, inspected, and placed in various data structures (e.g., lists or dictionaries) at run-time.
  • organizing code: package, module, class,method, function. 
  • flow control: if/else,while, iterator,generator, coroutine
  • error handling: try/except/finally
History of Python builtin types   
  • Influenced by ABC

python user defined class design
  • runtime data structure of instances and class, inheritance
    • instance dict, class dict
  • why "self" is needed
  • class implementation requires no special syntax: just a collection of functions (methods), variables (class variables), computed attributed(properties).  
  • new style classes
    • __new__(cls,*args,**kwargs) is used for change value for subclassing immutable type  
  • Attributes,Properties and Descriptors 
  • History of descriptor,staticmethod, classmethod, property
  • metaclass explained: metaclass 
    • type(name, bases, dict) Return a new type object. This is essentially a dynamic form of the class statement. Python will look for __metaclass__ in the class definition. If it finds it, if will use it to create the object class Foo. If it doesn't, it will use type to create the class.
       

object is first class
  • how to make method first class? via bound and unbound method
every statement is treated the same
  • benefit: share the same grammar rule, and hence the compiler could use the same byte code generation function for all of them.
exception design
  • early days implemented as string token (from Modula-3) 
  • user defined class as replacement


No comments:

Post a Comment