Lecture 8: Object Oriented Programming

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

21 Terms

1
New cards

Every Object has

  • a type

  • an internal data representation (primitive or composite)

  • a set of procedures for interaction with the object

2
New cards

Object

an instance of a type; basically all data in Python

3
New cards

Object Oriented Programming (OOP)

  • EVERYTHING IN PYTHON IS AN OBJECT (and has a type)

  • can create new objects of some type

  • can manipulate objects

  • can destroy objects (explicitly using del or just “forget” about them)

4
New cards

“garbage collection”

python system will reclaim destroyed or inaccessible objects

5
New cards

What are objects?

a data abstraction that captures…

  1. an internal representation through data attributes

  2. an interface for interacting with object through methods (aka procedures/functions). Defines behaviors but hides implementation

6
New cards

Advantages of OOP

  • bundle data into packages together with procedures that work on them through well-defined interfaces

  • divide-and-conquer development: implement and test behavior of each class separately; increased modularity reduces complexity

  • classes make it easy to reuse code: many Python modules define new classes; each class has a separate environment (no collision on function names); inheritance allows subclasses to redefine or extend a selected subset of a superclass’ behavior

7
New cards

Creating the Class involves

  • defining the class name

  • defining class attributes

  • for example, someone wrote code to implement a list class

8
New cards

Using the class involves

  • creating new instances of objects

  • doing operations on the instances

  • for example, L=[1,2] and len(L)

9
New cards

Defining your own types

  • use the class keyword to define a new type

  • similar to def, indent code to indicate which statements are part of the class definition

<ul><li><p> use the class keyword to define a new type</p></li><li><p>similar to def, indent code to indicate which statements are part of the class definition</p></li></ul><p></p>
10
New cards

What are attributes?

data and procedures that “belong” to the class

11
New cards

Data attributes

  • think of data as other objects that make up the class

  • for example, a coordinate is made up of two numbers

12
New cards

methods (procedural attributes)

  • think of methods as functions that only work with this class

  • how to interact with the object

  • for example you can define a distance between two coordinate objects but there is no meaning to a distance between two list objects

13
New cards

Defining how to create an instance of an class

  • first have to define how to create an instance of object

  • use a special method called __init_to initialize some data attributes

<ul><li><p>first have to define how to create an instance of object </p></li><li><p> use a special method called __<strong>init_</strong>to initialize some data attributes</p></li></ul><p></p>
14
New cards

Actually Creating an Instance of a Class?

  • data attributes of an instance are called instance variables

  • don’t provide argument for self, Python does this automatically

15
New cards

What is a method?

procedural attribute, like a function that works only with this class

16
New cards

“.” operator

is used to access any attribute ( a data attribute of an object and a method of an object)

17
New cards

How to use a method

knowt flashcard image
18
New cards

Print Representation of an Object

  • uninformative print representation by default

  • define a __str_ method for a class

  • Python calls the str method when used with print on your class object

  • you choose what it does!

19
New cards

isinstance()

use to check if an object is a instance of a particular class

20
New cards

Special Operators

allow you to customize classes and can add cool functionality to them; like print, can override these to work with your class; define them with double underscores before/after

<p>allow you to customize classes and can add cool functionality to them; like print, can override these to work with your class; define them with double underscores before/after</p>
21
New cards

The Power of OOP

  • bundle together objects that share: common attributes and procedures that operate on those attributes

  • use abstraction to make a distinction between how to implement an object vs how to use the object

  • build layers of object abstractions that inherit behaviors from other classes of objects and create our own classes of objects on top of Python’s basic classes