1/20
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Every Object has
a type
an internal data representation (primitive or composite)
a set of procedures for interaction with the object
Object
an instance of a type; basically all data in Python
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)
“garbage collection”
python system will reclaim destroyed or inaccessible objects
What are objects?
a data abstraction that captures…
an internal representation through data attributes
an interface for interacting with object through methods (aka procedures/functions). Defines behaviors but hides implementation
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
Creating the Class involves
defining the class name
defining class attributes
for example, someone wrote code to implement a list class
Using the class involves
creating new instances of objects
doing operations on the instances
for example, L=[1,2] and len(L)
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
What are attributes?
data and procedures that “belong” to the class
Data attributes
think of data as other objects that make up the class
for example, a coordinate is made up of two numbers
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
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
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
What is a method?
procedural attribute, like a function that works only with this class
“.” operator
is used to access any attribute ( a data attribute of an object and a method of an object)
How to use a method
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!
isinstance()
use to check if an object is a instance of a particular class
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
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