1. OOP and ADT

Object-oriented Design

Object-oriented design has fundamnental defferences from structured programming design methods. The two methods are similar in that they develop complex systems with divide and conquer, but differ in how to divide a given task.

Python Class and Access Modifier

Python Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Account:
# constructor
# 객체를 생성할 때 "반드시" 한 번 호출한다.
def __init__(self, cus_name, init_balance):
# instance member
self.name = cus_name
self.balance = init_balance

# descructor
# 객체가 소멸될 때 "반드시" 한 번 호출
def __del__(self):
pass

# instance method(operator)
def deposit(self, money):
if money < 0:
print('0보다 작은 값을 저금할 수 없습니다.')
return False

self.balance += money
print(f'잔고 {self.balance}')
return True

def withdraw(self, money):
if money > self.balance:
print('잔고보다 출금하려는 돈이 더 많습니다.')
return False

self.balance -= money
print(f'잔고 {self.balance}')
return money

def transfer(self, other, money):
self.balance -= money
# 다른 object의 member에 바로 접근하지 않는다!! (private없어서 접근 할 수는 있음(뭐임?))
# 다른 object의 member는 "반드시" 상대 object의 method를 호출해서 접근해야 한다. - Message Passing
other.deposit(money)
print(f'잔고 {self.balance}')

Object : abstraction method (추상화 도구)

  • 관련 있는 변수(member)와 기능(operator, method)를 묶어서 하나의 object로 만든다.
  • Operator를 통해서만 member에 접근할 수 있다.

Class와 Instance, object의 차이

  • 클래스(class)란 똑같은 무엇인가를 계속해서 만들어낼 수 있는 설계 도면 같은 것이고(과자 틀), 객체(object)란 클래스에 의해서 만들어진 피조물(과자틀에 의해서 만들어진 과자)을 뜻한다.
  • class에 의해서 만들어진 Object를 instance라고도 한다. 그렇다면 Object와 Instance의 차이는 무엇일까?
    이렇게 생각해 보자. a = Cookie() 이렇게 만들어진 a는 Object이다. 그리고 a라는 Object는 Cookie의 Instance이다.
  • 즉, Instance라는 말은 특정 Object(a)가 어떤 Class(Cookie)의 객체인지를 관계 위주로 설명할 때 사용된다. 즉, “a는 instance” 보다는 “a는 object”라는 표현이 어울리며, “a는 Cookie의 object” 보다는 “a는 Cookie의 instance”라는 표현이 훨씬 잘 어울린다.

Public, Private and Protected | Python Access Modifier

C++, Java와 같이 클래식한 object-oriented 언어에서는 public, private, protected와 같은 키워드로 class의 member에 대한 접근을 제어한다. Class의 Private member는 class 외부에서 접근이 불가능하며 class 내부에서만 접근될 수 있다.

Class 내의 Public member는 class 외부에서도 접근할 수 있다. 같은 class의 object는 public method를 호출하도록 요구된다. Private instance variable과 public method를 함께 써야지만 데이터 encapsulation의 원칙에 따르는 것이다.

Class의 protected member는 오직 해당 class와 그 class를 상속받은 child class만이 접근할 수 있다. 이는 parent class가 child class에게 특정한 리소스를 상속할 수 있게 한다.

그런데 Python은 instance variable과 method에 대해서 접근 제한을 하는 방식이 따로 없다. Python vaiable이나 method의 이름 앞에 한개 또는 2개의 _(underscore)를 붙여서 protected와 private으로 구분하기로 약속한다.

Public Attributes

1
2
3
4
5
class Account:
def __init__(self, cus_name, init_balance):
# instance member
self.name = cus_name
self.balance = init_balance

Protected Attribute

1
2
3
4
5
class Account:
def __init__(self, cus_name, init_balance):
# instance member
self._name = cus_name
self._balance = init_balance

Private Attribute

1
2
3
4
5
class Account:
def __init__(self, cus_name, init_balance):
# instance member
self.__name = cus_name
self.__balance = init_balance

그런데 웃긴 건, underscore로 name mangling한 private variable을 class 외부에서 접근이 가능하다는 것이다. 다음의 방법을 사용하면 된다.

1
2
myAccount = Account('subin', 10000)
myAccount._Account__balance

정말 필요하다면 접근할 수 있지만, Python에서는 접근하지 말 것을 권고하고 있다.


참고 자료

4 Fundamental of Object Oriented Programming

추상화 | Abstraction ?

  • 추상화란, 복잡한 로직을 가지고 있는 기능에서 그것을 다루기 위해 필요한 최소한의 핵심만을 추출해내는 것을 말한다. 정의만 들으면 어렵다.

    TV 전원을 예로 들어 생각해보자. 새로 산 TV의 설명서를 보면 TV를 켜려면 전원 버튼을 누르라고 되어있다. 사용자는 전원 버튼을 누르면 쉽게 TV를 켤 수 있다.

    그러나 실제로 TV의 전원 버튼을 누르는 순간 내부 전기회로에서는 복잡한 기능이 실행될 것이다. 사용자는 그것을 알 수 없고, 알 필요도 없다. TV 제작 회사에서 TV에 대한 추상화를 시켜 사용자가 쉽게 TV를 동작시킬 수 있도록 한 것이다.

  • 함수를 보통 function, routine, procedure 라고 부른다. 이 때, procedure 단위로 추상화를 하고 procdural하게 진행하는 프로그램을 절차 지향 프로그램이라고 한다. 그러나 프로그램이 거대해지고, 코드가 길어지자 프로그램을 객체(object)로 추상화하는 방법론이 나왔고 그걸 적용한 게 객체 지향 프로그램이다.

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×