Objects have both data and actions • data is contained in “properties” or “slots” • actions are defined in “methods” • The string 'John Doe' is an object in Python • The value 'John Doe' is contained in a default property • The methods are inherited by its definition as a type of str, for example upper, replace, and index 3 s = 'John Doe' typeof(s) => str # s is an object of type str print s => 'John Doe' # s contains the data 'John Doe' s.upper() => 'JOHN DOE' s.replace('John', 'Jane') => 'Jane Doe' s.index('D') => 5
which are prototypes for objects • Classes are important because custom objects are created by instantiating classes • When you create a list, python instantiates an object of type list • Class definitions provide methods and properties (slots) that will be created in the instantiated object: 4 l = [1, 2, 3, ] typeof(l) => list class Game(object): team1 = None team2 = None def __init__(self, team1, team2): self.team1 = team1 self.team2 = team2
(sometimes with arguments) like methods: • Methods and properties defined in the class exist in the instantiated object • self a special way that python allows an object to refer to itself (similar to this in Java and JavaScript • Methods defined in a Class must declare self as their first argument, but you don’t pass it in when you call them (this happens automatically) 5 >>> Game() TypeError: __init__() takes exactly 3 arguments (1 given) >>> game = Game('Team A', 'Team B') >>> game <__main__.Game at 0x10f1de910> >>> print game.team1 Team A
• Good OO modeling requires thinking about the logical structure of how things are organized • In class exercise: • Model a parking lot • Model a multi-level parking structure • Model a baseball game • Model a baseball team
OS X/Linux: ./test.py Write a stand-alone python program 8 #!/usr/bin/env python def test_python(s): print s if __name__ == '__main__': test_python('test')
it should have comments • Document each method your write, and intersperse comments describing why you are doing something • Clearly define what inputs you expect and what outputs you will provide • You will be reading and revising your own code, and after 6 months you won’t remember what you did and why! 9 def __resize__(self): """ Resizes the array by creating doubling the storage_length, creating a new list and re-hashing all stored items """ new_storage_length = self.storage_length * 2 # initialize a new list and pre-fill it with empty lists new_items = [[] for i in range(new_storage_length)] for item_list in self.items: ....
your code does what you believe it does: • Write unit tests • Pray • Writing units tests is the only way to “guarantee” that your code does what it is supposed to do • Unit tests are a form of software testing that validates code logic • They aren’t the only type of testing you need to do! • Unit tests are best at testing the outputs of a method for a variety of inputs • They must be thorough! 10
complete understanding of problem and solution • Write code that anyone can understand • Keep methods as short as possible (easier to test) • Refactor on a regular basis • Use variable names that are descriptive! • Test your code, even against unlikely circumstances • Read other people’s code as much as possible • This is the only way to become a good developer • Read about developing software • Challenge yourself • This is the only way to become a good developer • Most important: Have fun! 12
• Use python shell to call methods and assign variables • Import datetime and instantiate a datetime object • Use datetime instance methods • Write python code in a module and run it • Homework: • Write a Python method that: • Takes a list as its argument • Removes all negative and 0 value elements • Returns the list • Method should have comments (like all good code!) • Email me the method definition 13