DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 7

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

Berkeley COMPSCI 61A - Lecture 7

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 7
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 Lecture 7 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 Lecture 7 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?