0% found this document useful (0 votes)
45 views24 pages

CS229 Python Tutorial: TA: Mario Srouji

This document provides an overview of Python classes and object-oriented programming concepts through a tutorial on the CS229 Python class. It discusses why classes are useful for logical grouping, modeling techniques, and maintaining state. Key concepts covered include class definition, instantiation through __init__, self, instance methods, static methods, and inheritance including abstract classes.

Uploaded by

silent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views24 pages

CS229 Python Tutorial: TA: Mario Srouji

This document provides an overview of Python classes and object-oriented programming concepts through a tutorial on the CS229 Python class. It discusses why classes are useful for logical grouping, modeling techniques, and maintaining state. Key concepts covered include class definition, instantiation through __init__, self, instance methods, static methods, and inheritance including abstract classes.

Uploaded by

silent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS229 Python

Tutorial

TA: Mario Srouji


Python basics demo
Python OOP
Why Classes?
• Logical grouping of data and functions (which are called
methods)

• We try to create classes with logical connections or


unified functionality

• Modeling technique, a way of thinking about programs

• Very useful for maintaining “state” in programs

• Think of a class as a sort of “blueprint”


Class example
Class Instantiation
• The class Customer(object) line does not “create” the
class - this is defining the “blueprint”

• To instantiate the class - we call the __init__ method with


the proper number of arguments (minus self)

• __init__(self, name, balance=0.0)

• mario = Customer(“Mario Srouji”, 1000.0) - instantiates an


object mario of the class Customer
Class example
What does self mean?

• self is the instance of the class we are using

• When defining a function (method) inside of a class - need


to include self as first argument so we can use it

• Syntactical way to define that this particular method


should be applied to the given object instance

• mario.withdraw(100.0) = Customer.withdraw(mario, 100.0)


Class example
What does __init__ do?
• When we call __init__ we are creating the object instance

• It is the class “constructor”

• To call the __init__ method of a class, instantiate the class


name with the arguments defined in __init__

• mario = Customer(“Mario Srouji”, 1000.0)

• Variables or “attributes” defined in the __init__ method can


be accessed inside and outside the class

• mario.name = “bob” will modify the name attribute


Class example
Bad class example
Good practice

• Looked reasonable - calling the set_balance method


before using the instance of the class

• No way to communicate this to the user

• We can not force caller to invoke set_balance

• Rule of thumb - do not introduce an attribute outside of


the __init__ method
Instance Methods
• Function defined in a class is called a “method”

• Methods have access to all data contained in the instance


of the object

• Can access and modify anything previously defined on


self

• Since they use self, they require an instance of the class


to be used - hence we call them instance methods
Class example
Static Methods
• Do not have access to self

• Work without requiring an instance to be present

• Do not have a self parameter


Inheritance
• The process in which a “child” class derives data and
behavior from a “parent” class

• Avoids duplication of code - example in a moment

• Allows for creation of “abstract” classes - general


templates

• Can use “abstract” classes to define specific instances


depending on application
Why inheritance?
Why inheritance?
Why inheritance?

• The Car and Truck classes are almost identical -


unnecessary duplication of code

• They share a lot of data and functionality in common

• Why not introduce an abstraction that allows us to


combine these two Vehicle classes
Abstract classes

• The Vehicle class is a concept that allows us to embody


reusable information

• We can make the Car and Truck classes inherit from the
Vehicle class

• Let’s look at the example on the next slide


Abstract classes
Inheritance once again
Demo

You might also like