Object Oriented Programming Flashcards

Learn about how to create and use object oriented programming in Python

You may prefer our related Brainscape-certified flashcards:
1
Q

An object is an instance and has:

A
  • a type
  • an internal data representation
  • each instance is a particular type of object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Objects

A
  • A data abstraction that capture
  • Can create new instances of objects
  • Can destroy objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Data abstraction captures

A
  • internal representation

- interface for interacting with objects through methods (procedures)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Creating a class

A
  • defining the class name

- defining the class attributes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using the class

A
  • creating new instances of objects

- doing operations on the instance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Advantage of OOP

A
  • Bundle data into packages together with procedures (functions)
  • Divide-and-conquer development
  • Classes make it easy to reuse code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Divide-and-conquer development

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Method is

A

A function that works only with THIS class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

More about methods

A

Python always passes the actual object as the first argument to use as ‘self’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The “.” operator is used to:

A

Access any attribute:

  • a data attribute of an object
  • a method of an object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Class is

A

A blueprint created by a programmer for an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Object is

A

An instance of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Power of OOP

A
  • bundle objects together
  • use abstractions (how to implements vs. how to use)
  • build layers of object abstractions
  • create own classes of objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Implementing class
vs
using class
A

write code from two different perspectives

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Implementing an object

A
  • define the class
  • define the data attributes
  • define methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Using an object

A
  • create instances of object type

- do operations

17
Q

Class def. of an object

A
  • class is the type
  • class is defined generically (use self)
  • common across all instances
18
Q

Instance of a class

A
  • one particular object
  • data values vary between instances
  • structure of the class
19
Q

Why use OOP and classes of objects?

A
  • mimic real life

- group different objects as part of the same type

20
Q

Groups of objects have attributes

A
  • data attributes

- procedural attributes

21
Q

Data attributes

A
  • how can you represent your object with data

- what it is

22
Q

Procedural attributes

A

Behavior/operations/methods

  • what kind of things can you do with the object?
  • what it does
23
Q

Instance of DOT notation

A
  • instantiation creates an instance of an object
  • dot notation used to access attributes
  • better to use getters and setters!
24
Q

Information hiding

A
  • may change data attribute var. names
  • getting an error if:
    accessing data attributes outside class and class definition changes
25
Q

getters
and
setters

A

use a.get_xxx() NOT a.xxx

  • good style
  • easy to maintain code
  • prevent bugs
26
Q

Python not great at information hiding

A
  • 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!
27
Q

self and other args

A
  • self determined from instance

- default arguments for formal parameters

28
Q

Hierachies

A
  • parent class (super class)

- child class (sub class)

29
Q

Child class

A
  • inherits all data and behaviors
  • add more info
  • add more behavior
  • override behavior
30
Q

Which method to use

A
  • 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
31
Q

variables can be?

A
instance variables
or
class variables
32
Q

Instance variables

A
  • specific to an instance
  • created for each instance (belongs to an instance)
  • used the generic variable name ‘self’ within the class def.
33
Q

Class variables

A
  • belongs to a class
  • defined inside class
  • but outside the class __init__ methods
  • shared among all objects/instances of that class
34
Q

Summary of
classes
and
OOP

A
  • 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