SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
Object Oriented
                            Python



Tuesday, April 24, 12
Object Oriented
                            Python
                        The way programming was meant to be




Tuesday, April 24, 12
Who we are




Tuesday, April 24, 12
Who we are


                              Anna




Tuesday, April 24, 12
Who we are


                         David   Anna




Tuesday, April 24, 12
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
What is a class?
                         Factory for creating nouns
                         based on your description




Tuesday, April 24, 12
What is a class?
                         Factory for creating nouns
                         based on your description




Tuesday, April 24, 12
What does an object
                             contain?




Tuesday, April 24, 12
What does an object
                               contain?
                        Local Data




Tuesday, April 24, 12
What does an object
                               contain?
                        Local Data

                            Name: David
                            Birthday: Dec 18
                            Job: Software Engineer




Tuesday, April 24, 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
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
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
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
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
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
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
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
Banking Program
                           Objects we’ll need:

                               • Banks
                               • Users
                               • Accounts


Tuesday, April 24, 12
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
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
Classes & Objects




Tuesday, April 24, 12
Bank Class




Tuesday, April 24, 12
User Class




Tuesday, April 24, 12
Account Class




Tuesday, April 24, 12
Tuesday, April 24, 12
Main Program
                        •   program:

                            •   create bank object.

                            •   create user object.

                            •   log in.

                            •   create account object.

                            •   add money.



Tuesday, April 24, 12
Main Program




Tuesday, April 24, 12
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
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
Bank Class




Tuesday, April 24, 12
Tuesday, April 24, 12
Main Program




Tuesday, April 24, 12
What if changes
                        need to be made?


Tuesday, April 24, 12
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
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
Changes!
                        The government has just declared that
                          all bank deposits will be taxed %5.




Tuesday, April 24, 12
Changes!
                        The government has just declared that
                          all bank deposits will be taxed %5.

                                   Oh no!


Tuesday, April 24, 12
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
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
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
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
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
Any questions?

                        Classes?        Methods?

                             Objects?
                                        Syntax?
                        Local data?
Tuesday, April 24, 12

Contenu connexe

Dernier

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Oo python

  • 1. Object Oriented Python Tuesday, April 24, 12
  • 2. Object Oriented Python The way programming was meant to be Tuesday, April 24, 12
  • 3. Who we are Tuesday, April 24, 12
  • 4. Who we are Anna Tuesday, April 24, 12
  • 5. Who we are David Anna Tuesday, 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 description Tuesday, April 24, 12
  • 8. What is a class? Factory for creating nouns based on your description Tuesday, April 24, 12
  • 9. What does an object contain? Tuesday, April 24, 12
  • 10. What does an object contain? Local Data Tuesday, April 24, 12
  • 11. What does an object contain? Local Data Name: David Birthday: Dec 18 Job: Software Engineer Tuesday, 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, I'm " + 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, 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. 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. 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. 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. Banking Program Objects we’ll need: • Banks • Users • Accounts Tuesday, 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
  • 28. Main Program • program: • create bank object. • create user object. • log in. • create account object. • add money. Tuesday, 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
  • 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: 100 Tuesday, 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: 240 Tuesday, 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 - amount Tuesday, 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 - amount Tuesday, 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: 230 Tuesday, April 24, 12
  • 45. Any questions? Classes? Methods? Objects? Syntax? Local data? Tuesday, April 24, 12