6.
009
Fundamentals of Programming
Lecture 5:
Custom Types
Adam Hartz
[email protected]
6.009: Goals
Our goals involve helping you develop as a programmer, in
multiple aspects:
• Programming: Analyzing problems, developing plans
• Coding: Translating plans into Python
• Debugging: Developing test cases, verifying
correctness, finding and fixing errors
So we will spend time discussing (and practicing!):
• High-level design strategies
• Ways to manage complexity
• Details and “goodies” of Python
• A mental model of Python’s operation
• Testing and debugging strategies
6.009 Fundamentals of Programming Lecture 5 (slide 2) 20 Mar 2018
The Power of Abstraction
Thinking about complicated systems is complicated.
6.009 Fundamentals of Programming Lecture 5 (slide 3) 20 Mar 2018
The Power of Abstraction
Thinking about complicated systems is complicated.
Thinking about simpler systems is often simpler.
6.009 Fundamentals of Programming Lecture 5 (slide 4) 20 Mar 2018
The Power of Abstraction
Thinking about complicated systems is complicated.
Thinking about simpler systems is often simpler.
Framework for thinking about complicated systems:
• Primitives
• Means of Combination
• Means of Abstraction
• Recognizing meaningful Patterns
6.009 Fundamentals of Programming Lecture 5 (slide 5) 20 Mar 2018
The Power of Abstraction
Thinking about complicated systems is complicated.
Thinking about simpler systems is often simpler.
Framework for thinking about complicated systems:
• Primitives
• Means of Combination
• Means of Abstraction
• Recognizing meaningful Patterns
Example:
• Primitives: +, *, ==, !=, ...
• Combination: if, while, f(g(x)), ...
• Abstraction: def
6.009 Fundamentals of Programming Lecture 5 (slide 6) 20 Mar 2018
Custom Types très classy!
Python also provides a means of creating custom types:
the class keyword.
Today:
• Extending our notional machine to include classes
• What is self?
• Examples of creating new types and integrating them
into Python
6.009 Fundamentals of Programming Lecture 5 (slide 7) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
pass
6.009 Fundamentals of Programming Lecture 5 (slide 8) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
pass
v = Vec2D()
6.009 Fundamentals of Programming Lecture 5 (slide 9) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
pass
v = Vec2D()
v.x = 3
v.y = 4
6.009 Fundamentals of Programming Lecture 5 (slide 10) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
pass
v = Vec2D()
v.x = 3
v.y = 4
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
6.009 Fundamentals of Programming Lecture 5 (slide 11) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
pass
v = Vec2D()
v.x = 3
v.y = 4
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
print(mag(v))
6.009 Fundamentals of Programming Lecture 5 (slide 12) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
6.009 Fundamentals of Programming Lecture 5 (slide 13) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
v = Vec2D()
v.x = 3
v.y = 4
print(v.x)
print(v.ndims)
6.009 Fundamentals of Programming Lecture 5 (slide 14) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
v = Vec2D()
v.x = 3
v.y = 4
print(v.x)
print(v.ndims)
print(Vec2D.mag(v))
6.009 Fundamentals of Programming Lecture 5 (slide 15) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
v = Vec2D()
v.x = 3
v.y = 4
print(v.x)
print(v.ndims)
print(Vec2D.mag(v))
print(v.mag())
6.009 Fundamentals of Programming Lecture 5 (slide 16) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def __init__(self, x, y):
self.x = x
self.y = y
def mag(self):
return (self.x**2 + self.y**2) ** 0.5
6.009 Fundamentals of Programming Lecture 5 (slide 17) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def __init__(self, x, y):
self.x = x
self.y = y
def mag(self):
return (self.x**2 + self.y**2) ** 0.5
v = Vec2D(3, 4)
6.009 Fundamentals of Programming Lecture 5 (slide 18) 20 Mar 2018
Example: 2-D Vectors
class Vec2D:
ndims = 2
def __init__(self, x, y):
self.x = x
self.y = y
def mag(self):
return (self.x**2 + self.y**2) ** 0.5
v = Vec2D(3, 4)
print(v.mag())
6.009 Fundamentals of Programming Lecture 5 (slide 19) 20 Mar 2018
Integrating More Closely With Python
Python offers ways to integrate things more tightly into the
language: “magic” methods or “dunder” methods. For
example:
• print(x) is translated implicitly to print(x. str ())
• abs(x) is translated implicitly to x. abs ()
• x + y is translated implicitly to x. add (y)
• x - y is translated implicitly to x. sub (y)
• x[y] is translated implicitly to x. getitem (y)
• x[y] = z is translated implicitly to x. setitem (y, z)
For a full list, see:
https://docs.python.org/3/reference/datamodel.html, Section 3.3
Let’s add a couple of these to Vec2D and see the effects.
6.009 Fundamentals of Programming Lecture 5 (slide 20) 20 Mar 2018
The Rest of Today: More Examples
We’ll see how many we have time for...
• Linked List
• Polynomial
• Infinite List
• Memoized Function
6.009 Fundamentals of Programming Lecture 5 (slide 21) 20 Mar 2018