Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Object Oriented                            PythonTuesday, April 24, 12
Object Oriented                            Python                        The way programming was meant to beTuesday, April...
Who we areTuesday, April 24, 12
Who we are                              AnnaTuesday, April 24, 12
Who we are                         David   AnnaTuesday, April 24, 12
Why do I care about                              classes?                        • Code organization                      ...
What is a class?                         Factory for creating nouns                         based on your descriptionTuesd...
What is a class?                         Factory for creating nouns                         based on your descriptionTuesd...
What does an object                             contain?Tuesday, April 24, 12
What does an object                               contain?                        Local DataTuesday, April 24, 12
What does an object                               contain?                        Local Data                            Na...
What does an object                               contain?                        Local Data                            Na...
What does an object                               contain?                        Local Data                         Local...
What does an object                               contain?                        Local Data                         Local...
Syntax                        Definition        class User(object):          name = "Jack"          birthday = "Jan 2"     ...
Syntax                        Definition                       Usage                                                 >>> ja...
Syntax                         Definition                      Usage                                                 >>> ja...
Local Data      class User(object):                        >>> harry = User("Harry")        def __init__(self, name):     ...
Local Data      class User(object):                        >>> harry = User("Harry")        def __init__(self, name):     ...
Banking Program                           Objects we’ll need:                               • Banks                       ...
Main Program                            •   program:                                •bank classbank object.method.        ...
Main Program                            •   program:                                •bank classbank object.method.        ...
Classes & ObjectsTuesday, April 24, 12
Bank ClassTuesday, April 24, 12
User ClassTuesday, April 24, 12
Account ClassTuesday, April 24, 12
Tuesday, April 24, 12
Main Program                        •   program:                            •   create bank object.                       ...
Main ProgramTuesday, April 24, 12
Main Program                            •   program:                                •bank classbank object.method.        ...
Main Program                            •   program:                                •bank classbank object.method.        ...
Bank ClassTuesday, April 24, 12
Tuesday, April 24, 12
Main ProgramTuesday, April 24, 12
What if changes                        need to be made?Tuesday, April 24, 12
Banking Example        >>>         bank = Bank()        >>>         account_alice = Account(bank)        >>>         accou...
Banking Example        >>>         bank = Bank()                                                        >>>     account_al...
Changes!                        The government has just declared that                          all bank deposits will be t...
Changes!                        The government has just declared that                          all bank deposits will be t...
Changes!                          The government has just declared that                            all bank deposits will ...
Changes!                          The government has just declared that                            all bank deposits will ...
Original Version                        class Account(object):                           balance = 0                      ...
New Version                        class Account(object):                           balance = 0                          d...
Still Works        >>>         bank = Bank()                                                        >>>   account_alice.de...
Any questions?                        Classes?        Methods?                             Objects?                       ...
Upcoming SlideShare
Loading in …5
×

Oo python

1,317 views

Published on

A class on object-oriented python for beginners.

Published in: Technology
  • Be the first to comment

Oo python

  1. 1. Object Oriented PythonTuesday, April 24, 12
  2. 2. Object Oriented Python The way programming was meant to beTuesday, April 24, 12
  3. 3. Who we areTuesday, April 24, 12
  4. 4. Who we are AnnaTuesday, April 24, 12
  5. 5. Who we are David AnnaTuesday, 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 descriptionTuesday, April 24, 12
  8. 8. What is a class? Factory for creating nouns based on your descriptionTuesday, April 24, 12
  9. 9. What does an object contain?Tuesday, April 24, 12
  10. 10. What does an object contain? Local DataTuesday, April 24, 12
  11. 11. What does an object contain? Local Data Name: David Birthday: Dec 18 Job: Software EngineerTuesday, 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, Im " + 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, Im " + self.name >>> jack1.say_hello() def eat_spaghetti(self): "Hello, Im 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, Im " + self.name >>> jack1.say_hello() def eat_spaghetti(self): "Hello, Im Jack" print "Yum!" >>> jack2.eat_spaghetti() "Yum!" One class, multiple objectsTuesday, 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, Im Harry" def say_hello(self): >>> sally.say_hello() print "Hello, Im " + self.name "Hello, Im Sally" >>> harry.greet(sally) def greet(self, other): "Hi Sally, Im Harry!" print "Hi " + other.name + ", " + >>> sally.greet(harry) "Im " + self.name + "!" "Hi Harry, Im 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, Im Harry" def say_hello(self): >>> sally.say_hello() print "Hello, Im " + self.name "Hello, Im Sally" >>> harry.greet(sally) def greet(self, other): "Hi Sally, Im Harry!" print "Hi " + other.name + ", " + >>> sally.greet(harry) "Im " + self.name + "!" "Hi Harry, Im Sally!" >>> harry.name = "Frank" >>> harry.say_hello() "Hello, Im Frank!"Tuesday, April 24, 12
  20. 20. Banking Program Objects we’ll need: • Banks • Users • AccountsTuesday, 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 & ObjectsTuesday, April 24, 12
  24. 24. Bank ClassTuesday, April 24, 12
  25. 25. User ClassTuesday, April 24, 12
  26. 26. Account ClassTuesday, 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 ProgramTuesday, 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 ClassTuesday, April 24, 12
  33. 33. Tuesday, April 24, 12
  34. 34. Main ProgramTuesday, 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: 100Tuesday, 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: 240Tuesday, 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 - amountTuesday, 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 - amountTuesday, 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: 230Tuesday, April 24, 12
  45. 45. Any questions? Classes? Methods? Objects? Syntax? Local data?Tuesday, April 24, 12

×