61A Lecture 7Monday, September 12Pig Contest Rules•The score for an entry is the sum of win rates against every other entry.•All strategies must be deterministic functions of the current score! Non-deterministic strategies will be disqualified.•Winner: 3 points extra credit on Project 1•Second place: 2 points•Third place: 1 point•The real prize: honor and glory•To enter: submit a file pig.py that contains a function called final_strategy as assignment p1contest by Monday, 9/262Function Decorators(demo)3@trace1def triple(x): return 3 * xis identical todef triple(x): return 3 * xtriple = trace1(triple)Function decoratorDecorated functionWhy not just use this?The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsRevisions should require few code changesWriting fewer lines of code saves you timeIsolates problemsTesting functions stay smallCopy/Paste has a steep priceThese are guidelines,not strict rules!Practical guidanceFrom: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5>>> from operator import mul>>> def square(let): return mul(let, let)Not stylishPractical guidanceboolean turn_is_overd diceplay_helper take_turnFunctional Abstractions•Square takes one argument.•Square has the intrinsic name “square”.•Square computes the square of a number.•Square computes the square by calling mul.6def square(x): return pow(x, 2)def square(x): return mul(x, x-1) + xIf the name “square” were bound to a built-in function, sum_squares would still work identically YesNoYesNoWhat does sum_squares need to know about square to use it?def square(x): return mul(x, x)def sum_squares(x, y): return square(x) + square(y)Data7http://www.skyrill.com/seatinghabits/Front of the classroomStudent seating preferences at MITObjects(Demo)8•Representations of information•Data and behavior, bundled together to create...•Objects represent properties, interactions, & processes•Object-oriented programming: •A metaphor for organizing large programs•Special syntax for implementing classic ideasAbstractionsPython ObjectsIn Python, every value is an object.9• All objects have attributes• A lot of data manipulation happens through methods• Functions do one thing; objects do many related things• Use built-in objects to introduce ideas• Create our own objects using the built-in object system• Implement an object system using built-in objectsThe next four weeks:Native Data TypesProperties of native data types:1. There are primitive expressions that evaluate to native objects of these types.2. There are built-in functions, operators, and methods to manipulate these objects.10In Python, every object has a type.>>> type(today)<class 'datetime.date'>Numeric Data TypesThree numeric types in Python:>>> type(2)<class 'int'>>>> type(1.5)<class 'float'>>>> type(1+1j)<class 'complex'>11FourRepresents integersexactlyRepresents real numbers approximately(demo)Working with Real NumbersCare must be taken when computing with real numbers!(Demo)12Representing real numbers:False in a Boolean contexts:0011 1111 1101 0101 0101 0101 0101 0101 0101 0101 0101 0101 0101 0101 0101 01010000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00001000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000http://en.wikipedia.org/wiki/File:IEEE_754_Double_Floating_Point_Format.svg1/3 =Working with Real Numbers>>> def approx_eq_1(x, y, tolerance=1e-18): return abs(x - y) <= tolerance>>> def approx_eq_2(x, y, tolerance=1e-7): return abs(x - y) <= abs(x) * tolerance>>> def approx_eq(x, y): if x == y: return True return approx_eq_1(x, y) or approx_eq_2(x, y)>>> def near(x, f, g): return approx_eq(f(x), g(x))13or approx_eq_2(y,x)Moral of the StoryLife was better when numbers were just numbers!Having to know the details of an abstraction:•Makes programming harder and more knowledge-intensive•Creates opportunities to make mistakes•Introduces dependencies that prevent future changes14Coming Soon: Data
View Full Document