SlideShare a Scribd company logo
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

More Related Content

Recently uploaded

(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
Priyanka Aash
 
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
Priyanka Aash
 
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
Priyanka Aash
 
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Muhammad Ali
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
Safe Software
 
Step-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From ScratchStep-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From Scratch
softsuave
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
Shiv Technolabs
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
BrainSell Technologies
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
Google Developer Group - Harare
 
Uncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in LibrariesUncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in Libraries
Brian Pichman
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
bhumivarma35300
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Networks
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
sunilverma7884
 
The Impact of the Internet of Things (IoT) on Smart Homes and Cities
The Impact of the Internet of Things (IoT) on Smart Homes and CitiesThe Impact of the Internet of Things (IoT) on Smart Homes and Cities
The Impact of the Internet of Things (IoT) on Smart Homes and Cities
Arpan Buwa
 
Opencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of MünsterOpencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of Münster
Matthias Neugebauer
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
Priyanka Aash
 
Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17
Bhajan Mehta
 
Sonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdfSonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdf
SubhamMandal40
 

Recently uploaded (20)

(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
(CISOPlatform Summit & SACON 2024) Regulation & Response In Banks.pdf
 
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
(CISOPlatform Summit & SACON 2024) Workshop _ Most Dangerous Attack Technique...
 
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
(CISOPlatform Summit & SACON 2024) Orientation by CISO Platform_ Using CISO P...
 
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
 
Step-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From ScratchStep-By-Step Process to Develop a Mobile App From Scratch
Step-By-Step Process to Develop a Mobile App From Scratch
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
 
Uncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in LibrariesUncharted Together- Navigating AI's New Frontiers in Libraries
Uncharted Together- Navigating AI's New Frontiers in Libraries
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
 
IPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite SolutionIPLOOK Remote-Sensing Satellite Solution
IPLOOK Remote-Sensing Satellite Solution
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
 
The Impact of the Internet of Things (IoT) on Smart Homes and Cities
The Impact of the Internet of Things (IoT) on Smart Homes and CitiesThe Impact of the Internet of Things (IoT) on Smart Homes and Cities
The Impact of the Internet of Things (IoT) on Smart Homes and Cities
 
Opencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of MünsterOpencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of Münster
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
 
Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17Mule Experience Hub and Release Channel with Java 17
Mule Experience Hub and Release Channel with Java 17
 
Sonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdfSonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdf
 

Featured

Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
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
Pixeldarts
 
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
 
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
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
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
Neil Kimberley
 
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)
contently
 
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
Albert Qian
 
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)
 
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
Search Engine Journal
 
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
SpeakerHub
 
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
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
Tessa Mero
 
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
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 

Featured (20)

Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
 
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
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
 

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