Oo python

  • 897 views
Uploaded on

A class on object-oriented python for beginners.

A class on object-oriented python for beginners.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
897
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
10
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Object Oriented PythonTuesday, April 24, 12
  • 2. Object Oriented Python The way programming was meant to beTuesday, April 24, 12
  • 3. Who we areTuesday, April 24, 12
  • 4. Who we are AnnaTuesday, April 24, 12
  • 5. Who we are David AnnaTuesday, April 24, 12
  • 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. What is a class? Factory for creating nouns based on your descriptionTuesday, April 24, 12
  • 8. What is a class? Factory for creating nouns based on your descriptionTuesday, April 24, 12
  • 9. What does an object contain?Tuesday, April 24, 12
  • 10. What does an object contain? Local DataTuesday, April 24, 12
  • 11. What does an object contain? Local Data Name: David Birthday: Dec 18 Job: Software EngineerTuesday, April 24, 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. 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. 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. 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. 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. 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. 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. 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. Banking Program Objects we’ll need: • Banks • Users • AccountsTuesday, April 24, 12
  • 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. 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. Classes & ObjectsTuesday, April 24, 12
  • 24. Bank ClassTuesday, April 24, 12
  • 25. User ClassTuesday, April 24, 12
  • 26. Account ClassTuesday, April 24, 12
  • 27. Tuesday, April 24, 12
  • 28. Main Program • program: • create bank object. • create user object. • log in. • create account object. • add money.Tuesday, April 24, 12
  • 29. Main ProgramTuesday, April 24, 12
  • 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. 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. Bank ClassTuesday, April 24, 12
  • 33. Tuesday, April 24, 12
  • 34. Main ProgramTuesday, April 24, 12
  • 35. What if changes need to be made?Tuesday, April 24, 12
  • 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. 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. Changes! The government has just declared that all bank deposits will be taxed %5.Tuesday, April 24, 12
  • 39. Changes! The government has just declared that all bank deposits will be taxed %5. Oh no!Tuesday, April 24, 12
  • 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. 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. 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. 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. 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. Any questions? Classes? Methods? Objects? Syntax? Local data?Tuesday, April 24, 12