IST 338 today ! A whole new class of programming CS building blocks: functions and composition behind the CS curtain : circuits, assembly, loops The Designing Date class Data! ������ ������������ CS: theory + practice
Final Foziah: website using Django (or Flask) projects Not an end, but a beginning www.cs.hmc.edu/twiki/bin/view/CS5/IST338ProjectsPage2015
Mandelbrot Set! ex. cr. grading… ��������������� also: www.cs.hmc.edu/~jgrasel/
Classes and Objects
Everything in Python is an object ! Its capabilities depend on its class . functions type "methods" what's more, you can build your own...
Everything is an object? Take strings, for example: This calls the str constructor . >>> s = str( 42 ) >>> type(s) Shows the type of s is str <type 'str'> Shows all of the methods (functions) of s >>> dir(s) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] Let's try some!
Objects Like a list, an object is a container, but much more customizable: (1) Its data elements have names chosen by the programmer . (2) An object contains its own functions, called methods (3) In its methods, objects refer to themselves as self (4) Python signals special methods with two underscores: __init__ is called the constructor ; it creates new objects __repr__ tells Python how to print its objects I guess we should doubly underscore these two methods!
A Date object, d d 8 4 2015 day month year memory location ~ 42042778
A Date object, d d 8 4 2015 day month year memory location ~ 42042778 It's an alien date!
The Date class Date: """ a blueprint (class) for objects class that represent calendar days """ def __init__( self, mo, dy, yr ): """ the Date constructor """ self.month = mo self.day = dy what?! self.year = yr
The Date class Date: """ a blueprint (class) for objects class that represent calendar days """ This is the start of a new type called Date It begins with the keyword class This is the constructor for Date objects As is typical, it assigns input data to the data members. def __init__( self, mo, dy, yr ): """ the Date constructor """ self.month = mo self.day = dy self.year = yr These are data members – they are the information inside every Date object.
Date This is a class . It is a user-defined datatype that you'll build in Lab 10 this week… >>> d = Date(4,8,2015) Constructor! >>> d.month d contains data 4 members named day , month , and year >>> d.day 6 >>> d 04/08/2015 The repr! the repr esentation of an object of type Date The isLeapYear method returns True or False . >>> d.isLeapYear() How does it know what year to check ? False
class Date: The Date """ a blueprint (class) for objects that represent calendar days class """ def __init__( self, mo, dy, yr ): """ the Date constructor """ self.month = mo self.day = dy self.year = yr def __repr__( self ): """ used for printing Dates """ s = "%02d/%02d/%04d" % (self.month, self.day, self.year) ���������������� return s ������������� This is the repr for Date objects It tells Python how to print these objects. Why self instead of d ?
self is the variable calling a method >>> d = Date(4,8,2015) >>> d 04/08/2015 >>> d.isLeapYear() These methods need False access to the object that calls them: it's self >>> d2 = Date(1,1,2016) >>> d2 01/01/2016 >>> d2.isLeapYear() True
class Date: The Date """ a blueprint (class) for objects that represent calendar days class """ def __init__( self, mo, dy, yr ): """ the Date constructor """ self.month = mo self.day = dy self.year = yr def __repr__( self ): """ used for printing Dates """ s = "%02d/%02d/%04d" % (self.month, self.day, self.year) ���������������� return s ������������� def isLeapYear( self ): """ anyone know the rule? """ which are leap years?
class Date: def __init__( self, mo, dy, yr ): (constructor) def __repr__(self): (for printing) def isLeapYear( self ): """ here it is """ if self.year%400 == 0: return True if self.year%100 == 0: return False if self.year%4 == 0: return True return False
Special Dates?
Emily M
== vs. equals UFO license Area 51, CA What id is on your Date? >>> d = Date(11,12,2013) >>> d 11/12/2013 this constructs a different Date >>> d2 = Date(11,12,2013) >>> d2 11/12/2013 >>> d == d2 False How can this be False ?
Two Date objects: d 12 11 2013 day month year memory location ~ 42042 778 == compares memory locations , not contents originals underneath…
== vs. equals UFO license Area 51, CA What date is on your id ? What id is on your Date? >>> d = Date(11,12,2013) >>> d 11/12/2013 this constructs a different Date >>> d2 = Date(11,12,2013) >>> d2 11/12/2013 >>> d.equals(d2) True
class Date: equals def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self): ��������������� ��������� def equals(self, d2): """ returns True if they represent the same date; False otherwise ������������� """ self ���� if return True else: return False
>>> d = Date(1,1,2016) isBefore >>> d2 = Date(4,6,2015) >>> d.isBefore( d2 ) False class Date: def isBefore(self, d2): """ if self is before d2, this should return True; else False """ if self.year < d2.year: return True if self.year > d2.year: return False # here, the years are EQUAL! if self.month < d2.month: return True if self.month > d2.month: return False # here, the years and months are EQUAL! if self.day < d2.day: return True return False
I want even LESS ! isBefore class Date: def isBefore(self, d2): """ if self is before d2, this should return True; else False """ if [self.year,self.month,self.day] < [ d2.year, d2.month, d2.day] return True else: return False
Date 's purpose…?! always create with the >>> d = Date(5,14,2015) CONSTRUCTOR … >>> d 05/14/2015 the tomorrow method returns >>> d.tomorrow() nothing at all. Is it doing anything? >>> d d has changed! 05/15/2015 >>> d.subNDays(37) Why is this important? lots of printing, but no return value! Some methods return a value; others change the object that call it!
Lab today – or tomorrow Add these to your Date class! yesterday(self) tomorrow(self) addNDays(self, N) subNDays(self, N) isBefore(self, d2) isAfter(self, d2) diff(self, d2) dow(self) Prof. Benjamin ! no computer required… and use your Date class to analyze our calendar a bit…
Quiz Name(s) _____________________________ class Date: Don't return anything. def tomorrow(self): This CHANGES the date """ moves the date ahead 1 day """ object that calls it. DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31] DIM looks pretty first, add 1 to self.day _________ bright to me! self.day if self.day > ____________: then, adjust the month and year, but only if needed Implement tomorrow ! Extra: how could you make this work for leap years, too?
Quiz Name(s) _____________________________ class Date: Don't return anything. def tomorrow(self): This CHANGES the date """ moves the date ahead 1 day """ object that calls it. DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31] DIM looks pretty first, add 1 to bright to me! self.day then, adjust the month and year, only if needed Implement tomorrow ! Extra: how could you make this work for leap years, too?
Recommend
More recommend