python overview
python user defined class design
object is first class
- 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
- 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 classFoo
. If it doesn't, it will usetype
to create the class.
-
type(name, bases, dict) Return a new type object. This is essentially a dynamic form of the
class statement. Python will look for
object is first class
- how to make method first class? via bound and unbound method
- benefit: share the same grammar rule, and hence the compiler could use the same byte code generation function for all of them.
- early days implemented as string token (from Modula-3)
- user defined class as replacement
No comments:
Post a Comment