Object oriented programming¶

  • OOP is a way of structuring code to make it more reusable, maintainable, etc.

  • All libraries we use are using object oriented programming, therefore is good to understand the basic concepts

  • Explain that the input() is just

class User(object):

    def __init__(self):
        print("Provide first name: ")
        self.name = input()
        print("Provide last name: ")
        self.last = input()

    def __repr__(self):
        return '<User %r>' % self.name

    def __str__(self):
        return self.name
    
    def change_first_name(self):
        print("Modify the first name")
        self.name = input()
    
    def change_last_name(self):
        print("Modify the last name")
        self.last = input()
list = []
for user in range(2):
    user = User()
    list.append(user)
Provide first name: 
 Jose
Provide last name: 
 Urra
Provide first name: 
 Ashley
Provide last name: 
 Crian
print(list)
list[0].change_first_name()
'''
Run these commands step by step on the intepreter
'''
new_city = City(['c','Havana',-133.996669,190.000000,9500])
## check types (no need to use print in the python shell)
print(type(new_city))

## Explain the object stored in memory
print(new_city)
## Print a property of the city
print(new_city.name)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-e32e76a4f1a0> in <module>
      2 Run these commands step by step on the intepreter
      3 '''
----> 4 new_city = City(['c','Havana',-133.996669,190.000000,9500])
      5 ## check types (no need to use print in the python shell)
      6 print(type(new_city))

NameError: name 'City' is not defined
'''
These commands are meant to be used first in the python interpreter
'''
## Skip first this step and look at the error
from map import City

# 1. Skip this step and only doing after reflecting on error
# from map import Map

# 2. Create a city object from the given class
new_city = City(['c', 'Havana', 0.1, 2.6, 100000])

## 3. Inspect a city object
print(new_city.name)

## Create a map object
new_map = Map('oregonmap.csv')

## Inspect the Map