Tuesday, June 19, 2012

Diving into Python the Hard Way

   short attention span
   demand for quality/variety
   desire to learn Python
+ need for useful project
coolProject CreateProject (Dive Into Python, Python the Hard Way, Time)

First, some investigation...

What are Python's strengths? 

According to this:

Python code looks like executable pseudo code.
Can be used as an integration/extension language
Has strong (web) presence

Also
Python is interpreted and interactive
Doxygen-like "Sphinx" allows code to generate documentation
Supports high level datatypes (lists, tuples, dictionaries)

This page has a 'nice & concise' overview:  
Weakness = slower than C/C++/Java
Nimble language, meaning the "compiler and vm are the same program,"
     Load, Translate, Execute (no compiling, which allows optimization)

And Now for Foolish Young Optimism

Looking through basic examples, I don't feel like I need ( or can stomach ) the hand-holding, baby-step, tutorialize-me-to-death approach. Can't I just write something? Why not! 

A little more research into Python's core libraries and standard algorithms: 

Built In Functions (That I Didn't Automatically Assume ) Part I:
  • all( iterable ) - Are all the elements true? AND
  • any( iterable ) - ^ OR
  • callable( object ) - classes are callable (returning instances), class instances are callable if __call__() method is defined (Where/how/why would you use this??)
  • classmethod( function ) - returns the classmethod called function... see below for further investigation of classmethods and decorators etc.
  • cmp( x, y ) - compare two values a and b (answer is int of sign x-y )
  • complex( r, i ) - create complex number 
  • dir( [opt arg] ) - no args=> list variables in current scope; arg=>list valid attrib of arg

Decorators

David and Tom tried explaining this to me yesterday. Decorators are functions that return other functions, often "wrapping" them. 

Stack Overflow to the rescue -- HUGE answer halfway down the page (only read half, but I understand decorators now): 
You see, decorators are wrappers which means that they let you execute code before and after the function they decorate without the need to modify the function itself.
A decorator takes a function as a parameter, basically writes/defines the code to be run (before, yourFunction, after) and returns that as a new 'decorated' function. As for the syntax.... 

[define bow, giftwrap, box functions.... example: 

def box(func): 
   def wrapper():
       print "*bow on top goes here*"
   return wrapper()
]

@bow
@giftwrap
@box
def present(gift=defaultSocks)
     print gift

If you call present(), you get a pair of defaultSocks, in a box, wrapped, with a bow on top. TaDa! 


No comments:

Post a Comment