61a lecture 10
play

61A Lecture 10 Wednesday, September 25 2 Data Types Every value - PDF document

Announcements Homework 3 due Tuesday 10/1 @ 11:59pm Optional Hog Contest entries due Thursday 10/3 @ 11:59pm Composition scores will be assigned this week (perhaps by Monday). 3/3 is very rare on the first project. You can gain


  1. Announcements • Homework 3 due Tuesday 10/1 @ 11:59pm • Optional Hog Contest entries due Thursday 10/3 @ 11:59pm • Composition scores will be assigned this week (perhaps by Monday).  3/3 is very rare on the first project.  You can gain back any points you lose on the first project by revising it (November). 61A Lecture 10 Wednesday, September 25 2 Data Types Every value has a type (demo) Properties of native data types: 1. There are primitive expressions that evaluate to values of these types. Data 2. There are built-in functions, operators, and methods to manipulate those values. Numeric types in Python: >>> type(2) Represents integers exactly <class 'int'> >>> type(1.5) <class 'float'> Represents real numbers approximately >>> type(1+1j) <class 'complex'> 4 Objects • Objects represent information. • They consist of data and behavior, bundled together to create abstractions. • Objects can represent things, but also properties, interactions, & processes. • A type of object is called a class; classes are first-class values in Python. • Object-oriented programming: Data Abstraction • A metaphor for organizing large programs • Special syntax that can improve the composition of programs • In Python, every value is an object. • All objects have attributes. • A lot of data manipulation happens through object methods . • Functions do one thing; objects do many related things. (Demo) 5

  2. Data Abstraction Rational Numbers numerator • Compound objects combine objects together Programmers denominator All • A date: a year, a month, and a day • A geographic position: latitude and longitude Exact representation of fractions • An abstract data type lets us manipulate compound objects as units A pair of integers • Isolate two parts of any program that uses data: As soon as division occurs, the exact representation may be lost! Programmer  How data are represented (as parts) Great Assume we can compose and decompose rational numbers:  How data are manipulated (as units) • rational(n, d) returns a rational number x Constructor • Data abstraction: A methodology by which functions enforce an • numer(x) returns the numerator of x abstraction barrier between representation and use Selectors • denom(x) returns the denominator of x 7 8 Rational Number Arithmetic Rational Number Arithmetic Implementation 3 3 9 nx ny nx*ny nx ny nx*ny def mul_rational(x, y): * = * = * = return rational(numer(x) * numer(y), 2 5 10 dx dy dx*dy denom(x) * denom(y)) dx dy dx*dy Constructor Selectors Selectors def add_rational(x, y): 3 3 21 nx ny nx*dy + ny*dx nx ny nx*dy + ny*dx nx, dx = numer(x), denom(x) + = + = ny, dy = numer(y), denom(y) + = 2 5 10 dx dy dx*dy return rational(nx * dy + ny * dx, dx * dy) dx dy dx*dy def equal_rational(x, y): return numer(x) * denom(y) == numer(y) * denom(x) • rational(n, d) returns a rational number x These functions implement an Example General Form • numer(x) returns the numerator of x abstract data type for rational numbers • denom(x) returns the denominator of x 9 10 Pairs as Tuples >>> pair = (1, 2) A tuple literal: >>> pair Comma-separated expression (1, 2) >>> x, y = pair "Unpacking" a tuple >>> x 1 Pairs >>> y 2 >>> pair[0] Element selection 1 >>> pair[1] 2 >>> from operator import getitem >>> getitem(pair, 0) 1 >>> getitem(pair, 1) 2 More tuples next lecture 12

  3. Representing Rational Numbers Reducing to Lowest Terms Example: def rational(n, d): """Construct a rational number x that represents n/d.""" return (n, d) 2 1 1 3 5 5 * = + = Construct a tuple 2 3 2 5 10 2 from operator import getitem 15 1/3 5 25 1/25 1 def numer(x): * = * = """Return the numerator of rational number x.""" 6 1/3 2 50 1/25 2 return getitem(x, 0) def denom(x): """Return the denominator of rational number x.""" from fractions import gcd Greatest common divisor return getitem(x, 1) def rational(n, d): """Construct a rational number x that represents n/d.""" Select from a tuple g = gcd(n, d) return (n//g, d//g) 13 14 Abstraction Barriers Rational numbers as whole data values add_rational mul_rational equal_rational Rational numbers as numerators & denominators Abstraction Barriers rational numer denom Rational numbers as tuples tuple getitem However tuples are implemented in Python 16 Violating Abstraction Barriers Does not use Twice! constructors add_rational( (1, 2), (1, 4) ) Data Representations def divide_rational(x, y): return (x[0] * y[1], x[1] * y[0]) No selectors! And no constructor! 17

  4. What is Data? Behavior Conditions of a Pair To implement our rational number abstract data type, we used a two-element tuple. • We need to guarantee that constructor and selector functions work together to specify the right behavior. But is that the only way to make pairs of values? No! • Behavior condition : If we construct rational number x from numerator n Constructors, selectors, and behavior conditions: and denominator d, then numer(x)/denom(x) must equal n/d. • An abstract data type is some collection of selectors and constructors, If a pair p was constructed from elements x and y, then together with some behavior condition(s). • getitem_pair(p, 0) returns x, and • If behavior conditions are met, then the representation is valid. • getitem_pair(p, 1) returns y. Together, selectors are the inverse of the constructor You can recognize abstract data types by their behavior, not by their class Not true for rational numbers because of GCD Generally true of container types . (Demo) 19 20 Functional Pair Implementation Using a Functionally Implemented Pair point = pair(2, 4) >>> p = pair(1, 2) def pair(x, y): getitem_pair(point, 1) As long as we do not violate """Return a functional pair.""" the abstraction barrier, >>> getitem_pair(p, 0) def dispatch(m): we don't need to know that 1 if m == 0: pairs are just functions This function return x >>> getitem_pair(p, 1) represents a pair elif m == 1: 2 return y return dispatch Constructor is a If a pair p was constructed from elements x and y, then higher-order function • getitem_pair(p, 0) returns x, and def getitem_pair(p, i): • getitem_pair(p, 1) returns y. """Return the element at index i of pair p.""" return p(i) Selector defers to This pair representation is valid! the object itself 21 22 Example: http://goo.gl/9hVt8f

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend