Object Oriented Programming Flashcards
Learn about how to create and use object oriented programming in Python
An object is an instance and has:
- a type
- an internal data representation
- each instance is a particular type of object
Objects
- A data abstraction that capture
- Can create new instances of objects
- Can destroy objects
Data abstraction captures
- internal representation
- interface for interacting with objects through methods (procedures)
Creating a class
- defining the class name
- defining the class attributes
Using the class
- creating new instances of objects
- doing operations on the instance
Advantage of OOP
- Bundle data into packages together with procedures (functions)
- Divide-and-conquer development
- Classes make it easy to reuse code
Divide-and-conquer development
- implement and test behavior of each class separately
- increase modularity reduces complexity
- inheritance allows sub-classes to redefine or extend a subset of a super-class behavior
Method is
A function that works only with THIS class
More about methods
Python always passes the actual object as the first argument to use as ‘self’
The “.” operator is used to:
Access any attribute:
- a data attribute of an object
- a method of an object
Class is
A blueprint created by a programmer for an object
Object is
An instance of a class
Power of OOP
- bundle objects together
- use abstractions (how to implements vs. how to use)
- build layers of object abstractions
- create own classes of objects
Implementing class vs using class
write code from two different perspectives
Implementing an object
- define the class
- define the data attributes
- define methods
Using an object
- create instances of object type
- do operations
Class def. of an object
- class is the type
- class is defined generically (use self)
- common across all instances
Instance of a class
- one particular object
- data values vary between instances
- structure of the class
Why use OOP and classes of objects?
- mimic real life
- group different objects as part of the same type
Groups of objects have attributes
- data attributes
- procedural attributes
Data attributes
- how can you represent your object with data
- what it is
Procedural attributes
Behavior/operations/methods
- what kind of things can you do with the object?
- what it does
Instance of DOT notation
- instantiation creates an instance of an object
- dot notation used to access attributes
- better to use getters and setters!
Information hiding
- may change data attribute var. names
- getting an error if:
accessing data attributes outside class and class definition changes
getters
and
setters
use a.get_xxx() NOT a.xxx
- good style
- easy to maintain code
- prevent bugs
Python not great at information hiding
- allows you to access data from outside
- allows you to write to data from outside
- allows you to create data attributes for an instance from outside
- it’s not good style to do any of these!
self and other args
- self determined from instance
- default arguments for formal parameters
Hierachies
- parent class (super class)
- child class (sub class)
Child class
- inherits all data and behaviors
- add more info
- add more behavior
- override behavior
Which method to use
- subclass can have methods with the same name as super class
- subclass can have methods with the same name as other sub classes
- look for method name in current class def
- if not found -> look up the hierarchy
- use first method up the hierarchy
variables can be?
instance variables or class variables
Instance variables
- specific to an instance
- created for each instance (belongs to an instance)
- used the generic variable name ‘self’ within the class def.
Class variables
- belongs to a class
- defined inside class
- but outside the class __init__ methods
- shared among all objects/instances of that class
Summary of
classes
and
OOP
- bundle together objects
- common attributes and procedures
- use abstraction to make a distinction between
- how to implement
- how to use
- build layers of object abstractions
- create our own classes of objects