업무에 파이썬 활용할 줄 알기

Day16 | 중급 | 객체 지향 프로그래밍(OOP) 본문

Python/[Udemy] 100개의 프로젝트로 Python 개발 완전 정복

Day16 | 중급 | 객체 지향 프로그래밍(OOP)

SEO 데이터분석가 2023. 12. 27. 18:53

OOP는 왜 필요하고 어떻게 동작하는가?

OOP 사용법: 클래스와 객체

Waiter 모델을 만들어보자

has(attributes) is_holding_plate = True
tables_responsible = [4, 5, 6]
does(methods) def take_order(table, order):
      #takes order to chef

def take_payment(amout):
      #add money to restaurant

 

what the waiter has and what the waiter does are the two most important things that make up an object

 

an attribute is just a fancy word for a variable that's associated with a modeled object like our waiter here

it's a variable that's attached to a particular object

 

we call it a method because it's a function that a particular modeled object can do

we need a waiter object to take the order and we need a waiter object to take payment

these are not just free-floating functions

 

an object is just a way of combining some piece of data and some functionality altogether in the same thing

 

blueprint

we can actually generate multiple versions of the same object.

So we could have Henry who's waiter and we can also have Betty who's a waiter,

and we can generate as many of these as we want from the same blueprint.

an object.

so now let's take a look at how you use these class blueprints to create an actual object

Object: 객체

things they have: attributes는 변수(variables)의 또 다른 말

things they can do: methods

data와 functionality의 조합

객체를 만들고 속성과 메소드에 접근하기

코드로 object(객체)를 만드는 방법은 다음과 같다

what gets printed is a new turtle object from the turtle module and it's saved at this location in the computer's memory

string이나 number를 프린트 한 결과와는 다르다

 

이 object로 뭘 할 수 있는가

car

has (attributes) speed = 0
fuel = 32
does (methods) def move():
      speed = 60

def stop():
      speed = 0

 

 

PyPi를 이용해 파이썬 패키지를 추가하는 방법

객체 속성 변경 및 메소드 호출 연습

 

 

 

 

 

모범답안1

from menu import Menu
from coffee_maker import CoffeeMaker
from money_machine import MoneyMachine

money_machine = MoneyMachine()
coffee_maker = CoffeeMaker()
menu = Menu()

is_on = True

while is_on:
    options = menu.get_items()
    choice = input(f"What would you like? ({options}): ")
    if choice == "off":
        is_on = False
    elif choice == "report":
        coffee_maker.report()
        money_machine.report()
    else:
        drink = menu.find_drink(choice)
        
        if coffee_maker.is_resource_sufficient(drink) and money_machine.make_payment(drink.cost):
          coffee_maker.make_coffee(drink)

 

모범답안2

from menu import Menu
from coffee_maker import CoffeeMaker
from money_machine import MoneyMachine

money_machine = MoneyMachine()
coffee_maker = CoffeeMaker()
menu = Menu()

is_on = True

while is_on:
    options = menu.get_items()
    choice = input(f"What would you like? ({options}): ")
    if choice == "off":
        is_on = False
    elif choice == "report":
        coffee_maker.report()
        money_machine.report()
    else:
        drink = menu.find_drink(choice)
        is_enough_ingredients = coffee_maker.is_resource_sufficient(drink)
        is_payment_successful = money_machine.make_payment(drink.cost)
        if is_enough_ingredients and is_payment_successful:
          coffee_maker.make_coffee(drink)

 

질문

왜 MenuItem 클래스는 import하지도 않고, Object형태로 불러오지도 않는거지?

 

다시 정리하는 해당 수업의 목표 

커피머신 프로젝트를 그냥 코딩하는거랑 OOP(객체 지향 프로그래밍)하는 거랑 2가지 버전으로 코드를 작성해보았다

커피머신 프로젝트는 클래스를 내가 직접 만들어주는건 아니고

이미 만들어져있는 클래스의 attribution과 method를 작성하는 방법을 익히는 실습이었다