http://xkcd.com/. is 313 today schedule week 0: classes objects you're saying that...
TRANSCRIPT
![Page 1: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/1.jpg)
http://xkcd.com/
![Page 2: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/2.jpg)
IS 313 Today
Schedule
Week 0:
Classes
ObjectsYou're saying that
Python's got class?
Dictionaries and Classes
Week 1:
Week 2:
Where we've been…
Where we're going…
Week 4:
Week 5:
Week 6:
Week 8:
Week 9:
Week 10:
Week 12:
Week 13:
Week 14:
Projects
Functions and Data
Loops and
language
42
key value
a Dictionary object…
'a'
![Page 3: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/3.jpg)
Watch out!
http://www.youtube.com/watch?v=nBeUGqeYsQg
![Page 4: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/4.jpg)
Python ~ an object-oriented programming language
ClassesMethods
input
output
42
key value
a Dictionary object…
'a'"Oh yeah!"
a String object…
Objects
TypesFunctions
Data reigns
![Page 5: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/5.jpg)
Now it's data that's
royalty!
Almost all data in Java are OBJECTS
The CLASS of an object is its type.
METHODS are f'ns ~ "part of the
data"!
Some languages are ALL OBJECTS!
![Page 6: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/6.jpg)
Lists vs. DictionariesIf I had a dictionary, I
guess I could look up what it was!
Lists are not perfect…
You can't choose what to name data.
You have to start at 0.
Some operations can be slow for big lists …
L[0], L[1], …
LL[0] L[1]
reference
5 42
if 'tiger' in L:
L[1985] = 'ox'
L[1986] = 'tiger'
![Page 7: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/7.jpg)
More on dictionaries
Dictionaries have lots of built-in methods, or functions:
>>> d = {1990: 'horse', 1991: ram'}
>>> d.keys()
[ 1990, 1991 ]
>>> d.has_key( 1991 )
True
>>> d.has_key( 1969 )
False
>>> d.pop( 1988 )
'dragon'
They don't seem moronic to me!
pop deletes a key and returns its value
has_key checks if a key is present
keys returns a list of all keys
![Page 8: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/8.jpg)
There's madness in this method!Methods
Functions
d.has_key( 1991 )
are functions that are called by the data itself!
has_key( 1991, d )
all data must be passed in as function inputs…
are called on their own…Warning: this
has_key function is for example
purposes only. It does not exist!
![Page 9: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/9.jpg)
Classes and Objects
Design-it-yourself data.
![Page 10: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/10.jpg)
Software Engineering
creating and composing functions
Building atop the work of others…
Insight into details, e.g., data storage and retrieval.
loops and data handling
Invention, not reinvention.creating new
data structures
![Page 11: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/11.jpg)
Classes & Objects
An object-oriented programming language allows you to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
![Page 12: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/12.jpg)
Classes & Objects
An object-oriented programming language allows you to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
There will typically be MANY objects of a single class.
![Page 13: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/13.jpg)
Using objects and classes:
>>> d = dict()
>>> d['answer'] = 42
>>> dir(d)
all of the data members and methods of the dictionary class (and thus the object d !)
>>> d.keys()
[ 'answer' ]
>>> d.values()
[ 42 ]
A particularly appropriate reference …
two methods (functions) within all objects of class dictionary
an example of the dictionary constructor
s = str()dir!
![Page 14: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/14.jpg)
The CONSTRUCTOR …
class dict: """ a dictionary class """
def __init__( self ): """ the CONSTRUCTOR """ # sets all of the data needed # there's no data here!
Code
Commentsdefines a new datatype called
dict
the constructor is named __init__ but is used via the
class name
It sets all the DATA MEMBERS
of a new object
the object is called self
![Page 15: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/15.jpg)
The REPR …
class dict: """ a dictionary class """
def __repr__( self ): """ a grim method """ s = '{' for key in self.keys(): s += str(key) + ':' s += str( self[key] ) + ', ' s += '}' return s
Code
Commentsthis method is named __repr__ and it
provides a string that will be used when printing
the object being printed
is called self
the string to print gets returned
This code seems selfish to me...
![Page 16: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/16.jpg)
Do-it-yourself data structures!
class: your own TYPE of data
object: a variable of your own class type
data members: data an object contains
methods: functions an object contains
benefits: encapsulation & abstraction
Object-oriented programming
Blueprint for an object
variables of that type
An object is alive, responsible, and intelligent.
- C++ FAQs
![Page 17: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/17.jpg)
Examples…
Do reference libraries have
library references?
Python's class libraries… Graphics libraries
http://docs.python.org/lib/
>>> f = urllib.urlopen( 'http://www.cs.hmc.edu/~dodds/IS313/HW/index.html' )>>> text = f.read()>>> intro = text[:50]
![Page 18: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/18.jpg)
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
usually called "methods" instead of functions
User-defined structures that become part of the language (at least locally…)
![Page 19: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/19.jpg)
But Why ?
• Flexibility
• Reusability
• Abstraction
ordinary data structures
create-your-own
write once, take anywhere
worry once, use anywhere
![Page 20: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/20.jpg)
Date This is a class. It is a user-defined datatype that you'll build in the HW this week!
Are you asking what Date-a a Date
needs?
We want (or need) to represent lots of calendar days
The Problem
The Design What data should be set in the constructor?
What functionality will we need to support?
d.dow()usually some, but not all,
is known beforehand
![Page 21: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/21.jpg)
Using Dates
this is an object of type Date
>>> d = Date(1,1,2012)
>>> d
1/1/2012
this is a CONSTRUCTOR …
What does it do?
the representation of a particular object of type Date
>>> d.isLeapYear()True
>>> d2 = Date(11,1,2010)
>>> d2
11/1/2010
>>> d2.isLeapYear()
False
the isLeapYear method returns True or False. How does it know what year to check?
How does it know to return False, instead of True in this case ??
Another object of type Date - again, via the constructor.
![Page 22: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/22.jpg)
class Date:
def __init__( self, m, d, y ): """ the Date constructor """ self.month = m self.day = d self.year = y
def __repr__( self ): """ used for printing Dates """ s = "%02d/%02d/%04d" % (self.month, self.day, self.year) return s
def isLeapYear( self ):
The Date class
Why is everyone so far
away?!
![Page 23: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/23.jpg)
>>> d = Date(1,1,2012)
>>> d
1/1/2012
self
These methods need access to the
object that calls them
>>> d.isLeapYear()True
>>> d2 = Date(12,31,2011)
>>> d2
12/31/2011
>>> d2.isLeapYear()
False
is the specific OBJECT THAT CALLED THE METHOD !
These methods need access to the
object that calls them
Why not use d?
![Page 24: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/24.jpg)
Which leap is correct?
def isLeapYear( self ):
""" left-side leap """
if self.yr%400 == 0:
return True
elif self.yr%100 == 0:
return False
elif self.yr%4 == 0:
return True
else:
return False
def isLeapYear( self ):
""" right-side leap """
if self.yr%4 == 0:
return True
elif self.yr%100 == 0:
return False
elif self.yr%400 == 0:
return True
else:
return False
![Page 25: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/25.jpg)
a Leap of faith….
class Date: def __init__( self, mo, dy, yr ): (constructor) def __repr__(self): (for printing)
def isLeapYear( self ): """ here it is """ if self.yr%400 == 0: return True if self.yr%100 == 0: return False if self.yr%4 == 0: return True return False
How about a 4000-year rule?
![Page 26: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/26.jpg)
Date objects are mutable
>>> d = Date(1,1,2012)
>>> d
01/01/2012
always created with the CONSTRUCTOR …
>>> d.yesterday()
>>> d
12/31/2011
>>> d.tomorrow()
>>> d
01/01/2012
the yesterday method returns nothing at all. Is it doing anything?
Some methods return a value; others (like this one) change the object that call it!
d has changed!
watch out for changes!
![Page 27: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/27.jpg)
Date ids
>>> d = Date(11,1,2010)
>>> d
11/1/2010
>>> d2 = Date(11,2,2010)
>>> d2
11/2/2010
What date is on your id?
What id is on your Date?
>>> d == d2
>>> d2.yesterday()
>>> d == d2
Will these be true or false?
>>> d.equals( d2 )
![Page 28: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/28.jpg)
Double Date
>>> d2 = d
>>> d
11/1/2010
>>> d.yesterday()
>>> d2
>>> d2 == d
>>> d2.equals(d)
Excuse me -- ids please!
What happens
here?
![Page 29: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/29.jpg)
Using copy
>>> d2 = d.copy()
>>> d
10/31/2010
>>> d2
10/31/2010
>>> d.yesterday()
>>> d2
>>> d2 == d
But where does copy come from?
What happens
here?
![Page 30: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/30.jpg)
class Date: def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self):
def copy(self): """ returns a DIFFERENT object w/same date! """
def equals(self, d2): """ returns True if they represent the same date; False otherwise """
Where are these TWO inputs
coming from?
![Page 31: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/31.jpg)
class Date:
def isBefore(self, d2): """ True if self is before d2 """
if self.yr < d2.yr: return True
if self.mo < d2.mo: return True
if self.dy < d2.dy: return True
return False
What's wrong?
Why is this method WRONG for the dates
1/1/2011 11/2/2010
![Page 32: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/32.jpg)
class Date:
def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
![Page 33: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/33.jpg)
Add these methods to Date
no computer required…
Prof. Art Benjamin
HW today / tomorrow
copy(self)
equals(self, d2)
yesterday(self)
tomorrow(self)
addNDays(self, N)
subNDays(self, N)
isBefore(self, d2)
isAfter(self, d2)
diff(self, d2)
dow(self)
and use your Date class to analyze our calendar a bit…
hw6p
r1.p
yhw
6pr2
.py
![Page 34: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/34.jpg)
Unusual calendar years…
![Page 35: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/35.jpg)
Unusual calendar years…
This is why Russia's October Revolution of 1917, in fact, happened
in November!
At least they kept Valentine's Day...
![Page 36: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/36.jpg)
Upcoming calendar...Seems appropriate!
November 8
November 15
November 22
November 29
December 6
December 13
Classes and objects, part 2
Final project introductions
User interfaces + personal software engineering
No class (conference)
Project Presentations: progress so far
No class; projects are due this week.
![Page 37: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/37.jpg)
What's next?
Python has no Connect-four datatype…
| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|X| |O|--------------- 0 1 2 3 4 5 6
Can I see a demo?
… but we can correct that!Aargh!
![Page 38: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/38.jpg)
Lab time...
homework questions?other questions?
![Page 39: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/39.jpg)
![Page 40: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/40.jpg)
Try it…class Date:
def isBefore(self, d2): """ True if self is before d2 """
if self.yr < d2.yr: return True if self.mo < d2.mo: return True if self.dy < d2.dy: return True return False
def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
Why is this method WRONG for the dates
DIM might be helpful…
This tomorrow method should not return anything. It just CHANGES the date
object that calls it.
1/1/2011 11/2/2010
how might you fix this problem?
![Page 41: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/41.jpg)
Designing classes
1) What data? (Data Members)
2) What are objects' crucial capabilities? (Methods)
Not limited to 7x6!
![Page 42: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/42.jpg)
Connect Four: the object b
Boardb
intwidthstr str str
str str str
str str str
datalist str
str
str
data
intheight
What is the name of the method that will construct this data?
![Page 43: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/43.jpg)
Connect Four: constructor
class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height self.data = [] # this will be the board for row in range( 6 ): boardRow = [] for col in range( 7 ): boardRow += [' '] # add a space to this row self.data += [boardRow]
Bad magic?
![Page 44: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/44.jpg)
Connect Four: the object b
Boardb
intwidthstr str str
str str str
str str str
datalist str
str
str
intheight
| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|X| |O|--------------- 0 1 2 3 4 5 6
What is the name of the method that will print this data?
![Page 45: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/45.jpg)
def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( 6 ): s += '|' for col in range( 7 ): s += self.data[row][col] + '|' s += '\n'
return s
Connect Four: __repr__
To remove?
To add?
which row is row 0, row 1, and so on?
![Page 46: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/46.jpg)
Examplesdef addMove(self, col, ox):
row = self.height-1 while True: if self.data[row][col] == ' ': self.data[row][col] = ox
row -= 1
def allowsMove(self, col):
Step through this addMove method.
How does it work?
What's wrong?
a C4 board
col #'X' or 'O'
allowsMove should return True if col is a valid move;
False otherwise.
![Page 47: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/47.jpg)
C4 Board class: methods
__init__( self, width, height )
allowsMove( self, col )
__repr__( self )
addMove( self, col, ox )
isFull( self )
winsFor( self, ox )
the “constructor”
checks if allowed
places a checker
outputs a string
checks if any space is left
checks if a player has won
hostGame( self )play!
delMove( self, col )removes a checker
Which is trickiest… ?
![Page 48: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/48.jpg)
Checking wins… ?
Thoughts?
X O
b
corner cases?
![Page 49: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/49.jpg)
Lab … and hw questions
Thinking about final projects…
![Page 50: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/50.jpg)
There are several well-made, concise tutorials…
want to brush up on anything?
![Page 51: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/51.jpg)
Two good references for looking up syntax…
for checking out just one thing!
wikipedia, for sure http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/
![Page 52: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/52.jpg)
Aha! But what is this I see?
Constructors are giving me a HEAP of trouble!
"thinking like a computer"or, at least, like Java.
data
constructor(s)
![Page 53: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/53.jpg)
http://xkcd.com/
![Page 54: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/54.jpg)
IS 313 Today
Schedule
Week 0:
Classes
ObjectsYou're saying that
Python's got class?
Dictionaries and Classes
Week 1:
Week 2:
Where we've been…
Where we're going…
Week 4:
Week 5:
Week 6:
Week 8:
Week 9:
Week 10:
Week 12:
Week 13:
Week 14:
Projects
Functions and Data
Loops and
language
42
key value
a Dictionary object…
'a'
![Page 55: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/55.jpg)
Python ~ an object-oriented programming language
ClassesMethods
input
output
42
key value
a Dictionary object…
'a'"Oh yeah!"
a String object…
Objects
TypesFunctions
Data reigns
![Page 56: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/56.jpg)
Now it's data that's
royalty!
Almost all data in Java are OBJECTS
The CLASS of an object is its type.
METHODS are f'ns ~ "part of the
data"!
Some languages are ALL OBJECTS!
![Page 57: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/57.jpg)
There's madness in this method!Methods
Functions
d.has_key( 1991 )
are functions that are called by the data itself!
has_key( 1991, d )
all data must be passed in as function inputs…
are called on their own…Warning: this
has_key function is for example
purposes only. It does not exist!
![Page 58: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/58.jpg)
Software Engineering
creating and composing functions
Building atop the work of others…
Insight into details, e.g., data storage and retrieval.
loops and data handling
Invention, not reinvention.creating new
data structures
![Page 59: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/59.jpg)
Classes & Objects
An object-oriented programming language allows you to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
There will typically be MANY objects of a single class.
![Page 60: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/60.jpg)
Using objects and classes:
>>> d = dict()
>>> d['answer'] = 42
>>> dir(d)
all of the data members and methods of the dictionary class (and thus the object d !)
>>> d.keys()
[ 'answer' ]
>>> d.values()
[ 42 ]
A particularly appropriate reference …
two methods (functions) within all objects of class dictionary
an example of the dictionary constructor
s = str()dir!
![Page 61: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/61.jpg)
The CONSTRUCTOR …
class dict: """ a dictionary class """
def __init__( self ): """ the CONSTRUCTOR """ # sets all of the data needed # there's no data here!
Code
Commentsdefines a new datatype called
dict
the constructor is named __init__ but is used via the
class name
It sets all the DATA MEMBERS
of a new object
the object is called self
![Page 62: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/62.jpg)
The REPR …
class dict: """ a dictionary class """
def __repr__( self ): """ a grim method """ s = '{' for key in self.keys(): s += str(key) + ':' s += str( self[key] ) + ', ' s += '}' return s
Code
Commentsthis method is named __repr__ and it
provides a string that will be used when printing
the object being printed
is called self
the string to print gets returned
This code seems selfish to me...
![Page 63: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/63.jpg)
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
usually called "methods" instead of functions
User-defined structures that become part of the language (at least locally…)
![Page 64: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/64.jpg)
But Why ?
• Flexibility
• Reusability
• Abstraction
ordinary data structures
create-your-own
write once, take anywhere
worry once, use anywhere
![Page 65: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/65.jpg)
Date This is a class. It is a user-defined datatype that you'll build in the HW this week!
Are you asking what Date-a a Date
needs?
We want (or need) to represent lots of calendar days
The Problem
The Design What data should be set in the constructor?
What functionality will we need to support?
d.dow()usually some, but not all,
is known beforehand
![Page 66: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/66.jpg)
Using Dates
this is an object of type Date
>>> d = Date(1,1,2012)
>>> d
1/1/2012
this is a CONSTRUCTOR …
What does it do?
the representation of a particular object of type Date
>>> d.isLeapYear()True
>>> d2 = Date(11,1,2010)
>>> d2
11/1/2010
>>> d2.isLeapYear()
False
the isLeapYear method returns True or False. How does it know what year to check?
How does it know to return False, instead of True in this case ??
Another object of type Date - again, via the constructor.
![Page 67: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/67.jpg)
class Date:
def __init__( self, m, d, y ): """ the Date constructor """ self.month = m self.day = d self.year = y
def __repr__( self ): """ used for printing Dates """ s = "%02d/%02d/%04d" % (self.month, self.day, self.year) return s
def isLeapYear( self ):
The Date class
Why is everyone so far
away?!
![Page 68: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/68.jpg)
>>> d = Date(1,1,2012)
>>> d
1/1/2012
self
These methods need access to the
object that calls them
>>> d.isLeapYear()True
>>> d2 = Date(12,31,2011)
>>> d2
12/31/2011
>>> d2.isLeapYear()
False
is the specific OBJECT THAT CALLED THE METHOD !
These methods need access to the
object that calls them
Why not use d?
![Page 69: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/69.jpg)
a Leap of faith….
class Date: def __init__( self, mo, dy, yr ): (constructor) def __repr__(self): (for printing)
def isLeapYear( self ): """ here it is """ if self.yr%400 == 0: return True if self.yr%100 == 0: return False if self.yr%4 == 0: return True return False
How about a 4000-year rule?
![Page 70: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/70.jpg)
Date objects are mutable
>>> d = Date(1,1,2012)
>>> d
01/01/2012
always created with the CONSTRUCTOR …
>>> d.yesterday()
>>> d
12/31/2011
>>> d.tomorrow()
>>> d
01/01/2012
the yesterday method returns nothing at all. Is it doing anything?
Some methods return a value; others (like this one) change the object that call it!
d has changed!
watch out for changes!
![Page 71: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/71.jpg)
Date ids
>>> d = Date(11,1,2010)
>>> d
11/1/2010
>>> d2 = Date(11,2,2010)
>>> d2
11/2/2010
What date is on your id?
What id is on your Date?
>>> d == d2
>>> d2.yesterday()
>>> d == d2
Will these be true or false?
>>> d.equals( d2 )
![Page 72: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/72.jpg)
Double Date
>>> d2 = d
>>> d
11/1/2010
>>> d.yesterday()
>>> d2
>>> d2 == d
>>> d2.equals(d)
Excuse me -- ids please!
What happens
here?
![Page 73: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/73.jpg)
Using copy
>>> d2 = d.copy()
>>> d
10/31/2010
>>> d2
10/31/2010
>>> d.yesterday()
>>> d2
>>> d2 == d
But where does copy come from?
What happens
here?
![Page 74: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/74.jpg)
class Date: def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self):
def copy(self): """ returns a DIFFERENT object w/same date! """
def equals(self, d2): """ returns True if they represent the same date; False otherwise """
Where are these TWO inputs
coming from?
![Page 75: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/75.jpg)
class Date:
def isBefore(self, d2): """ True if self is before d2 """
if self.yr < d2.yr: return True
if self.mo < d2.mo: return True
if self.dy < d2.dy: return True
return False
What's wrong?
Why is this method WRONG for the dates
1/1/2011 11/2/2010
![Page 76: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/76.jpg)
class Date:
def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
![Page 77: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/77.jpg)
Add these methods to Date
no computer required…
Prof. Art Benjamin
HW today / tomorrow
copy(self)
equals(self, d2)
yesterday(self)
tomorrow(self)
addNDays(self, N)
subNDays(self, N)
isBefore(self, d2)
isAfter(self, d2)
diff(self, d2)
dow(self)
and use your Date class to analyze our calendar a bit…
hw6p
r1.p
yhw
6pr2
.py
![Page 78: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/78.jpg)
Unusual calendar years…
![Page 79: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/79.jpg)
Unusual calendar years…
This is why Russia's October Revolution of 1917, in fact, happened
in November!
At least they kept Valentine's Day...
![Page 80: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/80.jpg)
Upcoming calendar...Seems appropriate!
November 8
November 15
November 22
November 29
December 6
December 13
Classes and objects, part 2
Final project introductions
User interfaces + personal software engineering
No class (conference)
Project Presentations: progress so far
No class; projects are due this week.
![Page 81: Http://xkcd.com/. IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where](https://reader030.vdocuments.us/reader030/viewer/2022032606/56649e915503460f94b9690e/html5/thumbnails/81.jpg)
What's next?
Python has no Connect-four datatype…
| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|X| |O|--------------- 0 1 2 3 4 5 6
Can I see a demo?
… but we can correct that!Aargh!