Fndamentals of Programming and Aalgo Lecture 3 Notes
Fndamentals of Programming and Aalgo Lecture 3 Notes
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
Example:
• Primitives: +, *, ==, !=, ...
• Combination: if, while, f(g(x)), ...
• Abstraction: def
Today:
• Extending our notional machine to include classes
• What is self?
• Examples of creating new types and integrating them
into Python
class Vec2D:
pass
class Vec2D:
pass
v = Vec2D()
class Vec2D:
pass
v = Vec2D()
v.x = 3
v.y = 4
class Vec2D:
pass
v = Vec2D()
v.x = 3
v.y = 4
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
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))
class Vec2D:
ndims = 2
def mag(vec):
return (vec.x**2 + vec.y**2) ** 0.5
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)
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))
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())
class Vec2D:
ndims = 2
def mag(self):
return (self.x**2 + self.y**2) ** 0.5
class Vec2D:
ndims = 2
def mag(self):
return (self.x**2 + self.y**2) ** 0.5
v = Vec2D(3, 4)
class Vec2D:
ndims = 2
def mag(self):
return (self.x**2 + self.y**2) ** 0.5
v = Vec2D(3, 4)
print(v.mag())