Successfully reported this slideshow.

Oo python

1

Share

1 of 45
1 of 45

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Oo python

  1. 1. Object Oriented Python Tuesday, April 24, 12
  2. 2. Object Oriented Python The way programming was meant to be Tuesday, April 24, 12
  3. 3. Who we are Tuesday, April 24, 12
  4. 4. Who we are Anna Tuesday, April 24, 12
  5. 5. Who we are David Anna Tuesday, April 24, 12
  6. 6. Why do I care about classes? • Code organization • Readability • Making large-scale changes easier • What everybody does these days • Encapsulation (SAT word!) Tuesday, April 24, 12
  7. 7. What is a class? Factory for creating nouns based on your description Tuesday, April 24, 12
  8. 8. What is a class? Factory for creating nouns based on your description Tuesday, April 24, 12
  9. 9. What does an object contain? Tuesday, April 24, 12
  10. 10. What does an object contain? Local Data Tuesday, April 24, 12
  11. 11. What does an object contain? Local Data Name: David Birthday: Dec 18 Job: Software Engineer Tuesday, April 24, 12
  12. 12. What does an object contain? Local Data Name: David Birthday: Dec 18 Job: Software Engineer Actions say_hello() eat_spaghetti() teach_a_class() Tuesday, April 24, 12
  13. 13. What does an object contain? Local Data Local Data Name: David Name: Anna Birthday: Dec 18 Birthday: Sep 6 Job: Software Engineer Job: Entrepreneur Actions say_hello() eat_spaghetti() teach_a_class() Tuesday, April 24, 12
  14. 14. What does an object contain? Local Data Local Data Name: David Name: Anna Birthday: Dec 18 Birthday: Sep 6 Job: Software Engineer Job: Entrepreneur Actions say_hello() eat_spaghetti() teach_a_class() Tuesday, April 24, 12
  15. 15. Syntax Definition class User(object): name = "Jack" birthday = "Jan 2" job = "student" def say_hello(self): print "Hello, I'm " + self.name def eat_spaghetti(self): print "Yum!" Tuesday, April 24, 12
  16. 16. Syntax Definition Usage >>> jack1 = User() class User(object): >>> print jack1.name name = "Jack" "Jack" birthday = "Jan 2" job = "student" >>> jack2 = User() >>> print jack2.name def say_hello(self): "Jack" print "Hello, I'm " + self.name >>> jack1.say_hello() def eat_spaghetti(self): "Hello, I'm Jack" print "Yum!" >>> jack2.eat_spaghetti() "Yum!" Tuesday, April 24, 12
  17. 17. Syntax Definition Usage >>> jack1 = User() class User(object): >>> print jack1.name name = "Jack" "Jack" birthday = "Jan 2" job = "student" >>> jack2 = User() >>> print jack2.name def say_hello(self): "Jack" print "Hello, I'm " + self.name >>> jack1.say_hello() def eat_spaghetti(self): "Hello, I'm Jack" print "Yum!" >>> jack2.eat_spaghetti() "Yum!" One class, multiple objects Tuesday, April 24, 12
  18. 18. Local Data class User(object): >>> harry = User("Harry") def __init__(self, name): >>> sally = User("Sally") self.name = name >>> harry.say_hello() "Hello, I'm Harry" def say_hello(self): >>> sally.say_hello() print "Hello, I'm " + self.name "Hello, I'm Sally" >>> harry.greet(sally) def greet(self, other): "Hi Sally, I'm Harry!" print "Hi " + other.name + ", " + >>> sally.greet(harry) "I'm " + self.name + "!" "Hi Harry, I'm Sally!" Tuesday, April 24, 12
  19. 19. Local Data class User(object): >>> harry = User("Harry") def __init__(self, name): >>> sally = User("Sally") self.name = name >>> harry.say_hello() "Hello, I'm Harry" def say_hello(self): >>> sally.say_hello() print "Hello, I'm " + self.name "Hello, I'm Sally" >>> harry.greet(sally) def greet(self, other): "Hi Sally, I'm Harry!" print "Hi " + other.name + ", " + >>> sally.greet(harry) "I'm " + self.name + "!" "Hi Harry, I'm Sally!" >>> harry.name = "Frank" >>> harry.say_hello() "Hello, I'm Frank!" Tuesday, April 24, 12
  20. 20. Banking Program Objects we’ll need: • Banks • Users • Accounts Tuesday, April 24, 12
  21. 21. Main Program • program: •bank classbank object.method. create • create create user object. with log in •user class with name & password • create log in. •account class with amount • create create account object. • • add money. Tuesday, April 24, 12
  22. 22. Main Program • program: •bank classbank object.method. create • create create user object. with log in •user class with name & password • create log in. •account class with amount • create create account object. • • add money. Tuesday, April 24, 12
  23. 23. Classes & Objects Tuesday, April 24, 12
  24. 24. Bank Class Tuesday, April 24, 12
  25. 25. User Class Tuesday, April 24, 12
  26. 26. Account Class Tuesday, April 24, 12
  27. 27. Tuesday, April 24, 12
  28. 28. Main Program • program: • create bank object. • create user object. • log in. • create account object. • add money. Tuesday, April 24, 12
  29. 29. Main Program Tuesday, April 24, 12
  30. 30. Main Program • program: •bank classbank object.method. create • create create user object. with log in •user class with name & password • create log in. •account class with amount • create create account object. • • add money. Tuesday, April 24, 12
  31. 31. Main Program • program: •bank classbank object.method. create • create create user object. with log in •user class with name & password • create log in. •account class with amount • create create account object. • • add money. Tuesday, April 24, 12
  32. 32. Bank Class Tuesday, April 24, 12
  33. 33. Tuesday, April 24, 12
  34. 34. Main Program Tuesday, April 24, 12
  35. 35. What if changes need to be made? Tuesday, April 24, 12
  36. 36. Banking Example >>> bank = Bank() >>> account_alice = Account(bank) >>> account_alice.deposit(200) >>> account_bob = Account(bank) >>> account_bob.deposit(250) >>> account_carol = Account(bank) >>> account_carol.deposit(100) account_alice.balance: 200 account_bob.balance: 250 account_carol.balance: 100 Tuesday, April 24, 12
  37. 37. Banking Example >>> bank = Bank() >>> account_alice.deposit(100) >>> account_alice = Account(bank) >>> account_bob.deposit(50) >>> account_alice.deposit(200) >>> account_carol.withdraw(60) >>> account_bob = Account(bank) >>> account_alice.withdraw(200) >>> account_bob.deposit(250) >>> account_carol.deposit(200) >>> account_carol = Account(bank) >>> account_bob.withdraw(80) >>> account_carol.deposit(100) account_alice.balance: 100 account_bob.balance: 220 account_carol.balance: 240 Tuesday, April 24, 12
  38. 38. Changes! The government has just declared that all bank deposits will be taxed %5. Tuesday, April 24, 12
  39. 39. Changes! The government has just declared that all bank deposits will be taxed %5. Oh no! Tuesday, April 24, 12
  40. 40. Changes! The government has just declared that all bank deposits will be taxed %5. Oh no! Do we have to rewrite our entire program? Tuesday, April 24, 12
  41. 41. Changes! The government has just declared that all bank deposits will be taxed %5. Oh no! Do we have to rewrite our entire program? Nope! Tuesday, April 24, 12
  42. 42. Original Version class Account(object): balance = 0 def deposit(self, amount): self.balance = self.balance + amount def withdraw(self, amount): self.balance = self.balance - amount Tuesday, April 24, 12
  43. 43. New Version class Account(object): balance = 0 def deposit(self, amount): tax = amount * 5/100 remaining = amount - tax self.balance = self.balance + remaining def withdraw(self, amount): self.balance = self.balance - amount Tuesday, April 24, 12
  44. 44. Still Works >>> bank = Bank() >>> account_alice.deposit(100) >>> account_alice = Account(bank) >>> account_bob.deposit(50) >>> account_alice.deposit(200) >>> account_carol.withdraw(60) >>> account_bob = Account(bank) >>> account_alice.withdraw(200) >>> account_bob.deposit(250) >>> account_carol.deposit(200) >>> account_carol = Account(bank) >>> account_bob.withdraw(80) >>> account_carol.deposit(100) account_alice.balance: 95 account_bob.balance: 218.50 account_carol.balance: 230 Tuesday, April 24, 12
  45. 45. Any questions? Classes? Methods? Objects? Syntax? Local data? Tuesday, April 24, 12

×