Object Oriented Programming Flashcards
Why is object oriented programming important?
It is something that has revolutionized the way programming is done today. It also is great for code maintenance
In OOP classes have what two types of characteristics?
Attributes and Methods. What is has and what is can do. Variables inside a class and functions inside a class
Using the class, what can you create?
You can create different instances / different objects
Say you don’t want an instance of an object to have certain attributes?
You can use the private functionality to make the attribute or function not available to the object
Say you want to branch and upgrade the barrack?
You can utilize inheritance, naming the parent barrack as the parent class to inherit attributes
Describe ‘user’ use case for OOP
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 do you create a class in Python?
You can create by first keyword class then the name of your class followed by (Object)
class ClassName(object):
Classes include two types of things
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
Using a person example, walkthrough attributes and methods
I am a person. Like other people I have hair. Like other people I can walk
What is implicit passage of self?
Any method we create we need to include
What is self?
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.
__init__()
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
what is __init__() useful?
Because it allows us to set some attributes when a new instance is created. Because we know that the init method will run immediately.
Explain how to chain methods
If each method returns self, then you can chain your methods together.
What is DRY
don’t repeat yourself