owned this note
owned this note
Published
Linked with GitHub
###### tags: `Lesson Notes` `Python` `OOP` `PythonFlask2021`
# Classes
- So now we went over what the 4 pillars are and we did use classes but just what are they?
- How do they work?
- What are they used for?
## Blueprints:
- This is how we will need to start thinking of classes. When we were creating dictionaries we were basically creating a class.
- Each class has 1st a name to call it by like User, Dog, House
- Always capitalized
- After that we need to set up attributes. These are things that the instances (each object) have. Like a User has the attributes of a name, age, email for example
- Next we have methods. These are things that the instance can do. Like a User can take a walk, have a birthday
## Set up the class:
1. To start we need to declair that we are going to create a class
```
class House:
```
2. Now we need to know how we will create an instance at least to start with
```
winterHome = House()
summerHome = House()
```
3. Next is to add the attributes. Remember that is what each item will have.
```
class House:
def __init__(self):
```
- Before we go to much further it is important to note that each class will always start like this.
- Inside the () with self will go things that you will need to declair as you create the instance.
- Some attributes will not need to be declaired in each instance as it is always the same or is going to be part of a method.
- I will add both now to our House class
```python
class House:
def __init__(self, name, street, city, state, zip):
self.name = name
self.street = street
self.city = city
self.state = state
self.zip = zip
self.floors = 2
self.items = []
```
4. Ok now lets create an instance of our house
```
winterHome = House("Winter Home", "123 1st Street", "Berwick", "PA", 18603)
summerHome = House("Summer Home", "456 2nd Ave", "Virginia Beach", "VA", 23452)
```
5. Ok so we have 2 houses now. the self.floors means that every house we create will have 2 floors..this is a set value. The self.furniture is an empty list that we will use a method to add items to the house
1. 1st lets make a way to print the house
2. Then lets go ahead and add our stuff to the house
3. Maybe a way to print our items too
```
def printHouse(self):
print(f"Our {self.name} is located at {self.street} {self.city}, {self.state} {self.zip}. There are {self.floors}")
def addItems(self,item):
self.items.append(item)
def printItems(self):
print(f"Our {self.name} has the following items: {self.items}")
```
- Now we need to call these functions
```
winterHome.printHouse()
summerHome.printHouse()
winterHome.addItems('Couch')
winterHome.addItems('Bed')
summerHome.addItems('Pool')
summerHome.addItems('Grill')
summerHome.printItems()
winterHome.printItems()
```
So there you have it. Classes with attributes and methods.
# Link to Code:
- [Classes](https://pythontutor.com/visualize.html#code=class%20House%3A%0A%20%20%20%20def%20__init__%28self,%20name,%20street,%20city,%20state,%20zip%29%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20%20%20%20%20self.street%20%3D%20street%0A%20%20%20%20%20%20%20%20self.city%20%3D%20city%0A%20%20%20%20%20%20%20%20self.state%20%3D%20state%0A%20%20%20%20%20%20%20%20self.zip%20%3D%20zip%0A%20%20%20%20%20%20%20%20self.floors%20%3D%202%0A%20%20%20%20%20%20%20%20self.items%20%3D%20%5B%5D%0A%20%20%20%20%0A%20%20%20%20def%20printHouse%28self%29%3A%0A%20%20%20%20%20%20%20%20print%28f%22Our%20%7Bself.name%7D%20is%20located%20at%20%7Bself.street%7D%20%7Bself.city%7D,%20%7Bself.state%7D%20%7Bself.zip%7D.%20%20There%20are%20%7Bself.floors%7D%22%29%0A%20%20%20%20%0A%20%20%20%20def%20addItems%28self,item%29%3A%0A%20%20%20%20%20%20%20%20self.items.append%28item%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20def%20printItems%28self%29%3A%0A%20%20%20%20%20%20%20%20print%28f%22Our%20%7Bself.name%7D%20has%20the%20following%20items%3A%20%7Bself.items%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%0A%0AwinterHome%20%3D%20House%28%22Winter%20Home%22,%20%22123%201st%20Street%22,%20%22Berwick%22,%20%22PA%22,%2018603%29%0AsummerHome%20%3D%20House%28%22Summer%20Home%22,%20%22456%202nd%20Ave%22,%20%22Virginia%20Beach%22,%20%22VA%22,%2023452%29%0A%0AwinterHome.printHouse%28%29%0AsummerHome.printHouse%28%29%0A%0AwinterHome.addItems%28'Couch'%29%0AwinterHome.addItems%28'Bed'%29%0A%0AsummerHome.addItems%28'Pool'%29%0AsummerHome.addItems%28'Grill'%29%0A%0AsummerHome.printItems%28%29%0AwinterHome.printItems%28%29&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)