Day 16 Object-Oriented Programming 2¶

Nov. 10, 2020¶

CMSE logo

Administrative¶

  • Homework 4 is due Fri Nov 13
  • Projects Project groups assigned (Email and D2L)
  • Midterm is graded. Folks generally did well (91.5% average!).
  • No final exam

Project Groups¶

  • Tried to assign you to a group with your first/second choice
  • Not possible for everybody, but tried to keep same topical area
  • Groups have 3-5 people in them; based on mutual interests
  • You will be asked to share the contributions of each member of your group as part of the final project

Pre-Class¶

  • Not too much confusion, more questions of why?
  • A couple questions about __str__ vs __repr__
In [2]:
class Point(object):
   def __init__(self,x=0,y=0):
        self.x_coord = x
        self.y_coord = y
   def translate(self,x=0,y=0):
        self.x_coord += x
        self.y_coord += y
   def print_point(self):
        print("({:d},{:d})".format(self.x_coord, self.y_coord))
In [5]:
my_point = Point(1,2)
my_point.print_point()
(1,2)
In [6]:
 class Point(object):
   def __init__(self,x=0,y=0):
        self.x_coord = x
        self.y_coord = y
   def translate(self,x=0,y=0):
        self.x_coord += x
        self.y_coord += y
   def __str__(self):
        return "({:d},{:d})".format(self.x_coord, self.y_coord)
In [7]:
new_point = Point(2,3)
print(new_point)
(2,3)

__str__ vs __repr__¶

  • __str__ finds the informal (human readable) string
  • __repr__ find the formal (official) string
In [12]:
import datetime
now = datetime.datetime.now()
str(now)
Out[12]:
'2020-11-10 10:31:19.554885'
In [13]:
repr(now)
Out[13]:
'datetime.datetime(2020, 11, 10, 10, 31, 19, 554885)'

__str__ vs __repr__¶

str() repr()
Make object readable Required code that reproduces object
Generate output for end user Generate output for a developer

Questions, Comments, Concerns?¶