Module 4: Object-oriented programming Flashcards

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

what is OOP, what is it useful for, and how does it differ from other coding paradigms

A

There are different paradigms and ways of solving problems with code. Thus far, we have been writing procedural code, everything is step by step. We have also looked at functional programming, such as defining and passing in functions. OOP is another paradigm, it presents solutions to problems you encounter as your program gets longer and more complicated.
 Procedural Paradigm:
Useful for: Procedural programming is effective for tasks where the problem can be broken down into a sequence of steps or procedures that manipulate data. It is suitable for problems that involve a clear flow of control and where data is often mutable (changeable).
Examples: Simple scripts, command-line utilities, data processing tasks where you need to perform sequential operations on data (e.g., parsing files, data cleaning).
Example Problem: Parsing a log file to extract specific information and compute basic statistics like counts or averages.
 Object-Oriented Paradigm (OOP):
Useful for: OOP is beneficial for modeling complex systems where data and behavior are closely related and where you need to encapsulate state and operations on that state within objects. It focuses on building reusable and maintainable code through abstraction, inheritance, and polymorphism.
Examples: Large-scale applications, simulations, GUI applications, where you can model real-world entities as objects with properties (attributes) and behaviors (methods).
Example Problem: Designing a video game where different characters (objects) have distinct attributes (health, speed) and behaviors (movement, attacking).
 Functional Paradigm:
Useful for: Functional programming excels in scenarios where you need to process data using pure functions (no side effects) and emphasize immutable data structures. It focuses on transformations of data and the composition of functions.
Examples: Data transformation tasks, parallel processing, tasks involving complex data flows or pipelines.
Example Problem: Processing a stream of data (e.g., sensor data) where you need to filter, map, and reduce operations to derive meaningful insights or aggregate results.
Choosing the Right Paradigm
Functional Programming: Use it for data processing tasks, where transformations and avoiding side effects are important.
Procedural Programming: Suitable for smaller scripts or performance-critical tasks where clear step-by-step execution is needed.
Object-Oriented Programming: Opt for OOP when building larger applications or systems that involve complex interactions between different entities.

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

Basics of OOP, including concept, classes, and objects

A

OOP refers to a programming language model which defined objects containing data and functions that determine what types of operations can be applied to the data.

The core concepts are class which defines the data format and available procedures of an object. Any object is thus an instance of a corresponding class.

Classes:
The definitions for the data format and available procedures for a given type or class of object; may also contain data and procedures (known as class methods), i.e. classes contain the data members and member functions. The blueprint of a datatype.

Objects:
Instances of classes. A creation from the blueprint.

Think of a class as a template or a blueprint for any object created of this class.

Class Example
Field Examples
Functionality Examples
Vehicle
model, year, colour, type, transmission type
EngineStart(), accelerate(), EngineStop(), turn() and others

This class can define the following fields and functionality of a generalized vehicle. These functions are called methods, together fields and methods are called attributes of the class.

Using the class, we can create multiple objects of a class Vehicle, which will have different combinations and values of the attributes defined by the class. When a new object is created, we say that we created a new instance of a class.

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

Class and objects with a book example

A

This is going to allow us to create our own datatypes and give them names.

Let’s do an example of a class called Book, it will accept two parameters, an author and the title. The class will have a method which will print out the book description as a single sentence listing the title and author of the book.

A class in python is created using the keyword, class, then we type the name of the class we want, and a ( : ). The class body is indented four spaces.

class Book:
def __init__(self, author, title):
self.author = author
self.title = title
def bookdesc(self):
return “Book ‘{}’ by {}”.format(self.title, self.author)

The name of the method __init__() starts and ends with double underscores. This method is called automatically every time the new instance of a class is created. It initiates fields of the class, in our example it has 3 arguments, the argument self is always the first argument of a method in __init__() and it refers to the object itself which helps Python keep track of different instances of the class. The other two arguments in this case are the title of a book and author name, the function literally initializes the new objects by assigning initial values.

The function bookdesc prints out the name and title of the book, it also takes self as an argument.

bookA = Book(“George R.R. Martin”, “A Game of Thrones”)

Here we created an instance of the Book class by creating the bookA object:
Method __init__ used the string values and their locations to assign author and title – the instance attributes
The new object was assigned a variable bookA
The new object has a method bookdesk() which will print the title and author of a book – this is the instance method

type(bookA)
__main__.Book

bookA.author
‘George R.R. Martin’

bookA.title
‘A Game of Thrones’

bookA.bookdesc()
“Book ‘A Game of Thrones’ by George R.R. Martin”

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

Characteristics of python objects, including object type, object value, and object identity

A

Object type: tells python what kind of an object it’s dealing with. A type could be a string, list, or any other type of object, including custom-defined objects, like we saw above when we checked the type of object that the variable bookA is pointing to.
Object value: is the data value contained by the object. In the example above, it was the title of the book and name of an author. For an integer, for example, it can be the value of a number
Object identity: is the identity number for the object. Each distinct object in the computer’s memory will have its own identity number

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

what are the map and lambda functions

A

The map functions takes a function as one parameter and an iterable as the second.
Iterable: a python object we can loop over, lists, strings, tuples, dictionaries etc… returns an iterator object
Iterator: enables iteration over each element within an interable container

An interable has a built in method __iter__() which returns an iterator object. As iteratation happens over each element, the object returns the data one element at a time with the method __next__()

For example, if you have a list, you might need to loop through the list and apply a calculation defined by a function to each element of a list, or only to certain elements. Example below:

The map() returns an iterator object, so it in order to print out the values, we need to convert it to a list.

The square_it function is very short and can be replaced with the lambda function:
An anonymous function which does not have a name and thus cannot be re-used elsewhere – useful if we need to create a simple one-line custom function to another function

here instead of having a defined function that we call in our first parameter we just use the lambda function.

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