Object Oriented Programming Flashcards

1
Q

Why is object oriented programming important?

A

It is something that has revolutionized the way programming is done today. It also is great for code maintenance

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

In OOP classes have what two types of characteristics?

A

Attributes and Methods. What is has and what is can do. Variables inside a class and functions inside a class

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

Using the class, what can you create?

A

You can create different instances / different objects

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

Say you don’t want an instance of an object to have certain attributes?

A

You can use the private functionality to make the attribute or function not available to the object

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

Say you want to branch and upgrade the barrack?

A

You can utilize inheritance, naming the parent barrack as the parent class to inherit attributes

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

Describe ‘user’ use case for OOP

A

Let’s say we want to create a web app and will need lots of instances of users. We can start from scratch and create each user, but this clearly isn’t going to scale. Maybe we could have some sort of system for creating a user based upon a set of instructions. What attributes and actions are common across all instances of users?

It may seem like having a blueprint means that each user must be the same. The blueprint will only dictate what qualities (attributes) we must track for each user and what each user object should be able to do. When a new user is created, a value must be filled in for the name attribute.

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

How do you create a class in Python?

A

You can create by first keyword class then the name of your class followed by (Object)

class ClassName(object):

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

Classes include two types of things

A

Attributes - Characteristics shared by all instances of the class type. Take our User class, for example. All users have a name and an email. You might be wondering how each user can have a differet name and email

Methods - Actions that an object can perform. A user, for example, might be able to make a purchase. A method is like a function that belongs to a class. It’s some instructions that will only work when called on an object that has been created from that class.

Storage of information and ability to execute some logic

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

Using a person example, walkthrough attributes and methods

A

I am a person. Like other people I have hair. Like other people I can walk

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

What is implicit passage of self?

A

Any method we create we need to include

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

What is self?

A

Self is essentially a placeholder for the object. We can change the STATE of the SINGLE object by making modifications only to self. The self parameter includes all the information about the individual object that has called the method. Without self, every time we changed one’s object’s attributes, we’d change the attribute for ALL the items of that type.

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

__init__()

A

Python’s __init__method is something that’s known as a magic method. Magic methods are automatically created and sometimes invoked when a new instance of a class is created. One method you’ll be working with

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

what is __init__() useful?

A

Because it allows us to set some attributes when a new instance is created. Because we know that the init method will run immediately.

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

Explain how to chain methods

A

If each method returns self, then you can chain your methods together.

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

What is DRY

A

don’t repeat yourself

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

What are modules?

A

Simply python files with the .py extension which implement a set of functions. They are imported using the import command

17
Q

How do we use a function within a module?

A
We use dot notation after the module name
urllib.urlopen(..)
18
Q

How do you create your own module?

A

Create a new .py file with the module name in the same directory as the file that will import the module. Then we import it using the import command and the Python file name

19
Q

What are standard Built In Modules

A

Python comes with a library of standard modules. Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built-in, either for efficiency or to provide access to operating system primitives such as system calls.

20
Q

Are some modules exclusive to a platform?

A

Yes, like winreg module is only for Windows system. However, sys is built into every Python interpreter

21
Q

What are two important modules that come in handy when exploring modules in Python….what are they?

A

dir and help functions.

22
Q

What is a package?

A

A module is a single file (or files) that are imported under one import. A package is a collection of modules in directories that give a package hierarchy

23
Q

What must be in the package folder?

A

Each package in Python is a folder which MUST contain a special file called __init__.py. This file can be empty, and it indicates that the directory containing it is a Python package, so it can be imported the same way a module can be imported

24
Q

What are two ways to import a module within a package?

A
import my_modules.test_module
or
from my_modules import test_module
25
Q

How can you determine which modules to export as an API while keeping other moduesl internal?

A

The __init__.py file can also decide which modules this package will export as an API, while keeping other modules internal, by overriding the __all__variable, like so:

__init__.py:
__all__ = [“test_module”]

26
Q

What is inheritance

A

Is forming new classes using classes that have already been defined. In other words, it allos one class to take on some or even all of its attributes and methods from a parent class.

27
Q

What are the benefits of inheritance?

A

It allows one class to take on some or even all of its attributes and methods from a parent class. Code reuse and reductio of complexity of a program. THE DERIVE CLASSES (DESCENDANTS) CAN OVERRIDE OR EXTEND THE FUNCTIONALITY OF BASE CLASSES (ANCESTORS)

28
Q

When we defined each of our classes, we typed Bike(Vehicle), Car(Vehicle), Airplane(Vehicle). How could you read this in plain engligh?

A

“Make a class BIke/Car/Airplane that INHERITS from Vehicle”

This is known as implicit inheritance which allows us to use inherited attributes and methods of the Vehicle(parent) class in our new subclasses

29
Q

What is you want to pass in a variable number of arguments, or want to capture multiple arguments into a single parameter?

A

Placing an asterisk before the name of the parameter after the ‘normal’ parameters does just that. The asterisk is called a ‘splat’ operator. Bundle the remaining arguments into a new TUPLE, which is then assigned to that parameter

30
Q

What is super and what is it’s syntax?

A

Sometimes in your OOP you will want to create updates versions of methods that are defined in the parent class, because in addition to your custom code, you want specifically to call the parent implementation of that method as well (or instead). In these cases, you would reference that parent object with the keyword ‘super’

super(ChildClassName, self).parent_method()

31
Q

How can we change the child class attributes after it has inherited?

A

One thing we may want to do is call the Parent class’s __init__ method, but also have our Child class change attributes defined by its Parent class.