Unformatted text preview:

Python: a modern hybridPrototypingNo one will look at it without OOPWhat is looks likeSimple interactive modelUniform treatment of indexable dataSlicing (every which way) and iteratingDictionariesSet theory as a model of computationLoopsFunctionsModulesClasses and inheritanceData members are created implicitlyAny class can be a collectionExceptions, etc.Python: a modern hybrid•A language for scripting and prototyping•Balance between extensibility and powerful built-in data structures•genealogy:–Setl (NYU, J.Schwartz et al. 1969-1980)–ABC (Amsterdam, Meertens et al. 1980-)–Python (Van Rossum et all. 1996-)•Very active open-source communityPrototyping•Emphasis on experimental programming:•interactive (like LISP, ML, etc).•minimal translation to bytecode (like Java)•dynamic typing (like LISP, SETL, APL)•higher-order functions (LISP)•garbage-collected, no pointers (LISP, etc.)•Uniform treatment of indexable structures (like SETL)•Built-in associative structures (like SETL)•Light syntax, indentation is significant (from ABC)No one will look at it without OOP•Simple model of modules and classes•inheritance of implementation•multiple inheritance•No information-hiding•simple visibility model•clumsy mechanism for operator overloading•limited nesting:– built-in scope, global scope, local scopeWhat is looks like rulers = { ‘france’: [‘chirac’, 1995, 7], # general mapping ‘us’ : [‘clinton’, 1996, 4], ‘peru’ : [‘fujimori’, 1998, 0], “romania” : [‘illiescu’, 2000, 5]}for country in rulers.keys(): # built-in iterators [pres, elected, term] = rulers[country] # assignment if 2000 - elected < term: print country, “:”, pres “has %I years to go” % (term - (2000 - elected)) else: print country, “:”, pres, “is out of office”Simple interactive model$python pres.py # load and execute france: chirac has 2 years to go us: clinton is out of office romania: illiescu has 6 years to go peru: fujimori is out of office•can also write$python >>> import pres # load, execute, continueUniform treatment of indexable data•Strings, lists and arrays have common operations•characters are strings of length 1•name = “Python”;•courses = [“languages”, “compilers”] + [“databases”, “basketry”];•coordinates = (0.0, 1.5, -4.5. 2.0);•indexing from 0•negative index: indexing from end•name [-2] is “o”, courses [-3] is “compilers”•if ix is negative, lis [ix] is lis [len (lis) + ix]Slicing (every which way) and iterating•slicing: s[m:n] – from mth component, up to but excluding nth•s [m:] to end, •s[:n] from beginning, •s[:] all components•s * 4 repetition•built-in iterators:• for c in name: # c bound to each char• for course in courses:Dictionaries•General-purpose associative maps•domain (keys) of arbitrary types•retrieval by key:• rulers [‘peru’] yields [‘fujimori’, 1998, 0]•assignment / modification >>>rulers [‘peru’][2] = 10 # coup: another 8 years to go! >>>rulers [‘mexico’] = [‘fox’, 2000, 6] >>>rulers [‘israel’] = [ ] # no type consistency requiredSet theory as a model of computation•Alternative to lists + recursion: sets + membership + iterators•set constructors in SETL: S2 := { f (x) : x in S | P (x) };•in Python: S2 = [ ]; for x in S: if P(x): S2.append (f(x));Loops•Iterators over collections: for x in L:•Iterators over dictionaries for k in mydict.keys( ) …•Explicit iterators: for x in [1, 1, 2, 3, 5, 8, 13]:•Numeric iterators for x in range (1,100):Functionsdef intersect (seq1, seq2): # no type info res = [ ] # initialize list for x in seq1: # iterate over list if x in seq2: # built-in membership res.append (x) # in-place modification return res•assigned names are local unless declared global•no possible hidingModules•Modules are namespaces: unit of encapsulation•Modules are objects: components can be accessed•Modules can be inspected dynamically:– __dict__ provides dictionary for module:– keys are strings for entities for attr in module.__dict__keys ( ): # look at all entities print attr, # comma prevents LF if attr [0:2] == “__”: # naming convention print atrr, “built-in name”Classes and inheritance•Standard notions: superclasses, derived classes, self (for this), dynamic dispatching•Each class and each object is a namespace with a dictionary•To locate an operation, lookup in dictionary of object (dispatch table). If not found, examine superclasses.•Operator overloading through predefined names:–__init__ constructor–__del__ destructor–_add__ operator “+”–__repr__ printing, external representationData members are created implicitly Class Number: def __init__ (self, start): self.data = start; # data is defined def __add__ (self, other): # number + number return Number (self.data + other.data) def __repr__ (self) return `self.data` # convert to string•note: no way to overload (Number + integer) etc.Any class can be a collection class collection: def __getitem__ (self, i): return self.data[I] # attribute data is indexable …. X = collection (); X.data = [1, 2, 3] # member exists, assignment ok for item in X: # for calls __getitem print item << item # equivalent to item * 2 ^ itemExceptions, etc.•internally, iterator implemented as i = 0; try: while 1: # no boolean type item = getitem (self, i) ... # body of loop i = i +1 except IndexError: # eventually i too large pass # null statement except: # all other exceptions print “unexpected


View Full Document

NYU CSCI-GA 2110 - Python - a modern hybrid

Download Python - a modern hybrid
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Python - a modern hybrid and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Python - a modern hybrid 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?