πŸ“š μŠ€ν„°λ””/파이썬 μŠ€ν„°λ”” κ°•μ˜μžλ£Œ

[2νŒ€/κΉ€κ°€λ¦Ό, μ΅œλ‹€μ˜ˆ] 8μ°¨μ‹œ 파이썬 μŠ€ν„°λ”” - 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°

λ‹€μ˜ˆλ» 2023. 5. 18. 12:25

8μ°¨μ‹œ_객체지ν–₯ν”„λ‘œκ·Έλž˜λ°_κ°•μ˜μ•ˆ.pdf
2.21MB
8μ°¨μ‹œ_객체지ν–₯ν”„λ‘œκ·Έλž˜λ°_과제.pdf
0.07MB

μ°Έκ³ λ„μ„œ : 데이터 과학을 μœ„ν•œ 파이썬 ν”„λ‘œκ·Έλž˜λ°, μ΅œμƒμ² 

 

 

객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ„ λ°°μš°λŠ” 이유

  • λ‹€λ₯Έ μ‚¬λžŒμ΄ μž‘μ„±ν•œ μ½”λ“œλ₯Ό μ–΄λ–»κ²Œ μ‚¬μš©ν•˜λ©΄ 쒋을 지에 λŒ€ν•œ λ‹΅
  • 남이 λ§Œλ“  μ½”λ“œλ₯Ό 재 μ‚¬μš©ν•˜κ³  싢을 λ•Œ μ‚¬μš©ν•˜λŠ” λŒ€ν‘œμ μΈ 방법
  • ν•˜λ‚˜μ˜ 단일 ν”„λ‘œκ·Έλž¨μ„ 객체라고 ν•˜λŠ” μ½”λ“œλ‘œ λ§Œλ“€μ–΄ μž¬μ‚¬μš©ν•  수 μžˆλ„λ‘

객체와 클래슀

  • 객체 : μ‹€μƒν™œμ— μ‘΄μž¬ν•˜λŠ” μ‹€μ œμ μΈ 물건 λ˜λŠ” κ°œλ…
  • 속성과 ν–‰λ™μœΌλ‘œ ꡬ성
  • 클래슀 : 객체가 κ°€μ Έμ•Ό ν•  κΈ°λ³Έ 정보λ₯Ό 담은 μ½”λ“œλ‘œ μΌμ’…μ˜ 섀계도 μ½”λ“œ

객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ€ μ΄λŸ¬ν•œ 객체의 κ°œλ…μ„ ν™œμš©ν•˜μ—¬ ν”„λ‘œκ·Έλž¨μ„ ν‘œν˜„ν•˜λŠ” 기법

속성 → λ³€μˆ˜, 행동 → ν•¨μˆ˜λ‘œ μ •μ˜λ¨

ex) 인곡지λŠ₯ 좕ꡬ ν”„λ‘œκ·Έλž¨

μ–΄λ–€ μ’…λ₯˜μ˜ 객체가 ν•„μš”ν•œμ§€

  • ν•˜λ‚˜μ˜ ν”„λ‘œκ·Έλž¨μ—μ„œ μ—¬λŸ¬ 개의 객체가 μ‚¬μš©λ  수 있음 → 객체듀을 μœ„ν•œ 섀계도λ₯Ό λ§Œλ“€μ–΄μ•Ό 함 (클래슀 : 객체가 κ°€μ Έμ•Ό ν•  κΈ°λ³Έ 정보λ₯Ό 담은 μ½”λ“œ, 섀계도 μ½”λ“œ)
  • 좕ꡬ μ„ μˆ˜λΌλŠ” 객체λ₯Ό λ§Œλ“ λ‹€λ©΄ μ„ μˆ˜μ˜ 이름, ν¬μ§€μ…˜, μ†Œμ†νŒ€ 등은 기본적 속성
  • μ΄λŸ¬ν•œ 정보λ₯Ό ν΄λž˜μŠ€μ— → 객체에 μ„ μˆ˜ 이름을 ν• λ‹Ή (μΈμŠ€ν„΄μŠ€)

잘 λ§Œλ“  λΆ•μ–΄λΉ΅ν‹€ → μƒˆλ‘œμš΄ μ’…λ₯˜μ˜ λ‹€μ–‘ν•œ λΆ•μ–΄λΉ΅

잘 λ§Œλ“  클래슀 μ½”λ“œ → λ‹€μ–‘ν•œ μ’…λ₯˜μ˜ μΈμŠ€ν„΄μŠ€ 생성


클래슀 κ΅¬ν˜„ν•˜κΈ°

πŸ’‘ 상속 : 기쑴에 λ§Œλ“  클래슀의 νŠΉμ§•μ„ κ·ΈλŒ€λ‘œ 이어받아 μ‚¬μš©ν•˜λŠ” 것

객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ μž₯점 쀑 ν•˜λ‚˜λŠ” μž¬μ‚¬μš©μ΄κ³ , 이λ₯Ό μ†μ‰½κ²Œ ν•˜λŠ” 것이 상속

πŸ’‘ 클래슀 이름을 μ„ μ–Έν•  λŒ€λŠ” 첫 κΈ€μžμ™€ 쀑간 κΈ€μžκ°€ λŒ€λ¬Έμžμž„

 

μ†μ„±μ˜ μ„ μ–Έ

파이썬 클래슀의 λŒ€ν‘œμ μΈ μ˜ˆμ•½ ν•¨μˆ˜λŠ” init(), 이 외에 str**(),** add() 등이 쑴재

μ΄λŸ¬ν•œ ν•¨μˆ˜λ“€μ€ μ•½μ†λœ ν˜•νƒœμ˜ μž‘μ—…μ„ μˆ˜ν–‰

class SoccerPlayer(object):
    def__init__(self, name, position, back_number):
        self.name = name
        self.position = position
        self.back_number = back_numbe
  • init() : 속성에 λŒ€ν•œ 정보 μ„ μ–Έ, ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©ν•  λ³€μˆ˜λ₯Ό μ •μ˜ν•˜λŠ” ν•¨μˆ˜
  • init() ν•¨μˆ˜μ˜ 첫 번째 λ§€κ°œλ³€μˆ˜λŠ” λ°˜λ“œμ‹œ self λ³€μˆ˜λ₯Ό μ‚¬μš©ν•΄μ•Ό 함
  • self λ³€μˆ˜λŠ” ν΄λž˜μŠ€μ—μ„œ μƒμ„±λœ μΈμŠ€ν„΄μŠ€μ— μ ‘κ·Όν•˜λŠ” μ˜ˆμ•½μ–΄, μƒμ„±λœ μΈμŠ€ν„΄μŠ€λ₯Ό μ§€μ •ν•˜λŠ” λ³€μˆ˜
  • self λ’€μ˜ λ§€κ°œλ³€μˆ˜λ“€μ€ μ‹€μ œλ‘œ ν΄λž˜μŠ€κ°€ 가진 속성self.name = name → ν• λ‹Ή μ½”λ“œ
  • μƒμ„±λœ μΈμŠ€ν„΄μŠ€μ— μžˆλŠ” name λ³€μˆ˜μ— λ§€κ°œλ³€μˆ˜λ‘œ μž…λ ₯된 nameμ΄λΌλŠ” 값을 ν• λ‹Ήν•œλ‹€λŠ” 뜻
  • 이 값듀은 μ‹€μ œλ‘œ μƒμ„±λ˜λŠ” μΈμŠ€ν„΄μŠ€μ— 할당됨
  • 클래슀의 λ³€μˆ˜λŠ” ‘self.λ³€μˆ˜μ΄λ¦„’으둜 init() ν•¨μˆ˜μ—μ„œ 자유둭게 생성 κ°€λŠ₯But, init() λ‚΄μ—μ„œλ§Œ μƒˆλ‘œμš΄ 속성을 μƒμ„±ν•΄μ•Όλ§Œ λ‹€λ₯Έ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ 클래슀λ₯Ό μ‚¬μš©ν•  λ•Œ ν—·κ°ˆλ¦¬μ§€ μ•Šμ„ 것
  • νŒŒμ΄μ¬μ€ 인터프리터 언어이고 동적 타이핑 μ–Έμ–΄μ΄λ―€λ‘œ, 클래슀 λ‚΄ λ‹€λ₯Έ ν•¨μˆ˜μ—μ„œλ„ 이와 같은 μ†μ„±μ˜ 생성이 κ°€λŠ₯

ν•¨μˆ˜μ˜ μ„ μ–Έ

ν•¨μˆ˜λŠ” ν΄λž˜μŠ€κ°€ ν•  수 μžˆλŠ” λ‹€μ–‘ν•œ λ™μž‘μ„ μ •μ˜ν•  수 있음

class SoccerPlayer(object):
    def change_back_number(self, new_number):
        print("μ„ μˆ˜μ˜ λ“±λ²ˆν˜Έλ₯Ό λ³€κ²½ν•œλ‹€: From %d to %d" % (self.back_number, new_number))
        self.back_number = new_number

κΈ°μ‘΄ ν•¨μˆ˜μ™€ κ°€μž₯ 큰 차이점은 selfλ₯Ό λ§€κ°œλ³€μˆ˜μ— λ°˜λ“œμ‹œ λ„£μ–΄μ•Ό ν•œλ‹€λŠ” 것

selfκ°€ μžˆμ–΄μ•Όλ§Œ μ‹€μ œλ‘œ μΈμŠ€ν„΄μŠ€κ°€ μ‚¬μš©ν•  수 μžˆλŠ” ν•¨μˆ˜λ‘œ 선언됨

_(underscore)의 μ“°μž„

κ°œμˆ˜μ— 따라 μ“°μž„μ΄ λ‚˜λ‰¨

  1. 1개

μ΄ν›„λ‘œ 쓰이지 μ•Šμ„ λ³€μˆ˜μ— νŠΉλ³„ν•œ 이름을 λΆ€μ—¬ν•˜κ³  싢지 μ•Šμ„ λ•Œ

>>> for _ in range(10):
        print("Hello, World")
Hello, World
Hello, World
Hello, World
Hello, World
Hello, World
Hello, World
Hello, World
Hello, World
Hello, World
Hello, World

횟수λ₯Ό μ„ΈλŠ” _ λ³€μˆ˜λŠ” νŠΉλ³„ν•œ μš©λ„κ°€ μ—†μœΌλ―€λ‘œ λ’€μ—μ„œ μ‚¬μš©λ˜μ§€ μ•ŠμŒ

_λ₯Ό μž„μ˜μ˜ λ³€μˆ˜λͺ… λŒ€μ‹ μ— μ‚¬μš©

 

   2. 2개

νŠΉμˆ˜ν•œ μ˜ˆμ•½ ν•¨μˆ˜λ‚˜ λ³€μˆ˜

  • str() : 클래슀둜 μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν–ˆμ„ λ•Œ, κ·Έ μΈμŠ€ν„΄μŠ€ 자체λ₯Ό print() ν•¨μˆ˜λ‘œ 화면에 좜λ ₯ν•˜λ©΄ λ‚˜μ˜€λŠ” 값을 λœ»ν•¨
  • init()

μΈμŠ€ν„΄μŠ€ μ‚¬μš©ν•˜κΈ°

ν΄λž˜μŠ€μ—μ„œ μ‹€μ œμ μΈ 데이터가 μž…λ ₯λ˜μ–΄ μ‚¬μš©ν•  수 μžˆλŠ” ν˜•νƒœμ˜ 객체

SoccerPlayerλΌλŠ” ν΄λž˜μŠ€λŠ” κΈ°λ³Έ μ„€κ³„λ„μ΄λ―€λ‘œ μ„ μˆ˜μ˜ μ΄λ¦„μ΄λ‚˜ λ“± λ²ˆν˜Έκ°€ ν• λ‹Ήλœ 것이 μ•„λ‹˜

  • 클래슀 이름 ν˜ΈμΆœν•˜κ³ , init() ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜μ— λ§žμΆ”μ–΄ 값을 μž…λ ₯ (μ΄ˆκΉƒκ°’ 지정)
  • self λ³€μˆ˜μ—λŠ” μ•„λ¬΄λŸ° 값도 ν• λ‹Ήν•˜μ§€ μ•ŠμŒ
  • jinhyunμ΄λΌλŠ” μ΄λ¦„μ˜ μΈμŠ€ν„΄μŠ€κ°€ SoccerPlayerλΌλŠ” 클래슀λ₯Ό 기반으둜 생 μ„±
  • jinhyunμ΄λΌλŠ” μΈμŠ€ν„΄μŠ€ μžμ²΄κ°€ SoccerPlayer ν΄λž˜μŠ€μ—μ„œ self에 할당됨
# 전체 SoccerPlayer μ½”λ“œ
>>> class SoccerPlayer(object):
        def __init__(self, name, position, back_number):
            self.name = name
            self.position =position
            self.back_number = back_number
        def change_back_number(self, new_number):
            print("μ„ μˆ˜μ˜ λ“±λ²ˆν˜Έλ₯Ό λ³€κ²½ν•œλ‹€: From %d to %d" % (self.back_number, new_number))
            self.back_number = new_number
        def __str__(self):
            return "Hello, My name is %s. I play in %s in center." % (self.name, self.position)

# SoccerPlayerλ₯Ό μ‚¬μš©ν•˜λŠ” instance μ½”λ“œ
>>> jinhyun = SoccerPlayer("Jinhyun", "MF", 10)
>>>
>>> print("ν˜„μž¬ μ„ μˆ˜μ˜ λ“±λ²ˆν˜ΈλŠ”:", jinhyun.back_number)
>>> jinhyun.change_back_number(5)
>>> print("ν˜„μž¬ μ„ μˆ˜μ˜ λ“±λ²ˆν˜ΈλŠ”:", jinhyun.back_number)
>>> print(jinhyun)       # __str__() ν•¨μˆ˜λ‘œ μ„ μ–Έλœ 뢀뢄이 print() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ λ°˜ν™˜λ˜λŠ” ν•¨μˆ˜

ν˜„μž¬ μ„ μˆ˜μ˜ λ“±λ²ˆν˜ΈλŠ”: 10
μ„ μˆ˜μ˜ λ“±λ²ˆν˜Έλ₯Ό λ³€κ²½ν•œλ‹€: From 10 to 5
ν˜„μž¬ μ„ μˆ˜μ˜ λ“±λ²ˆν˜ΈλŠ”: 5 
Hello, My name is Jinhyun. I play in MF in center.

μΈμŠ€ν„΄μŠ€κ°€ μƒμ„±λœ ν›„μ—λŠ” ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€μ˜ μ΄λ¦„μœΌλ‘œ 값을 ν• λ‹Ήν•˜κ±°λ‚˜ ν•¨μˆ˜λ₯Ό λΆ€λ₯΄λ©΄ λ˜μ§€λ§Œ, 클래슀 λ‚΄μ—μ„œλŠ” self둜 호좜됨

= μƒμ„±λœ μΈμŠ€ν„΄μŠ€μΈ jinhyunκ³Ό 클래슀 λ‚΄ selfκ°€ 같은 μ—­ν• 


클래슀λ₯Ό μ‚¬μš©ν•˜λŠ” 이유

  • μžμ‹ μ˜ μ½”λ“œλ₯Ό λ‹€λ₯Έ μ‚¬λžŒμ΄ μ†μ‰½κ²Œ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ„€κ³„ν•˜κΈ° μœ„ν•¨
  • μ½”λ“œκ°€ 데이터 μ €μž₯ 뿐만 μ•„λ‹ˆλΌ 데이터λ₯Ό λ³€ν™˜ν•˜κ±°λ‚˜ λ°μ΄ν„°λ² μ΄μŠ€μ— μ €μž₯ν•˜λŠ” λ“±μ˜ λ‹€λ₯Έ 역할을 ν•΄μ•Ό ν•  λ•Œλ„ 있음 → ν•˜λ‚˜μ˜ 객체둜 생성해 λ‹€λ₯Έ μ‚¬λžŒλ“€μ—κ²Œ λ°°ν¬ν•˜λŠ” 것이 훨씬 μž‘μ—…μ΄ 쉬움
  • λ‹¨μˆœνžˆ 데이터λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•œ μˆ˜λ‹¨ → 이차원 리슀트, Namedtuple ν˜•νƒœλ₯Ό μ‚¬μš©ν•΄λ„ 문제 μ—†μŒ
  • μ½”λ“œλ₯Ό μ’€ 더 μ†μ‰½κ²Œ μ„ μ–Έν•  수 있음
  • 이차원 리슀트둜 μ„ μ–Έν•  수 μžˆλŠ” 것을 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ κ°œλ…μ„ μ μš©ν•΄ μ’€ 더 λͺ…ν™•ν•˜κ²Œ μ €μž₯된 데이터λ₯Ό 확인할 수 있음
  • λ‹€λ₯Έ μ‚¬λžŒλ“€μ΄ κ²°κ³Όλ₯Ό μ‚¬μš©ν•  λ•Œ 데이터가 무엇을 μœ„ν•œ 데이터인지 λͺ…ν™•νžˆ μ•Œ 수 있음
# 데이터
>>> names = ["Messi", "Ramos", "Ronaldo", "Park", "Buffon"]
>>> positions = ["MF", "DF", "CF", "WF", "GK"]
>>> numbers = [10, 4, 7, 13, 1]

# 이차원 리슀트
>>> players = [[name, position, number] for name, position, number in zip(names, positions, numbers)]
>>> print(players)
[['Messi', 'MF', 10], ['Ramos', 'DF', 4], ['Ronaldo', 'CF', 7], ['Park', 'WF', 13], ['Buffon', 'GK', 1]]
>>> print(players[0])
['Messi', 'MF', 10]

# 전체 SoccerPlayer μ½”λ“œ
>>> class SoccerPlayer(object):
        def __init__(self, name, position, back_number):
            self.name = name
            self.position = position
            self.back_number = back_number
        def change_back_number(self, new_number):
            print("μ„ μˆ˜μ˜ λ“±λ²ˆν˜Έλ₯Ό λ³€κ²½ν•œλ‹€: From %d to %d" % (self.back_number, new_number))
            self.back_number = new_number
        def __str__(self):
            return "Hello, My name is %s. I play in %s in center." % (self.name,self.position)

# 클래슀-μΈμŠ€ν„΄μŠ€
>>> player_objects = [SoccerPlayer(name, position, number) for name, position, number in zip(names, positions, numbers)]
>>> print(player_objects[0])
Hello, My name is Messi. I play in MF in center.

객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ° 만λŠ₯주의’λ₯Ό μ£Όμ˜ν•˜λΌ

λͺ¨λ“  μ½”λ“œμ— 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ κ°œλ…μ„ μ‚¬μš©ν•΄ μ„€κ³„ν•˜κ³ μž ν•˜λŠ” μš•μ‹¬μ΄ 생김

μ“°κ³  λ²„λ¦¬κ²Œ λ˜λŠ” μ½”λ“œλ₯Ό κ°œλ°œν•˜λŠ” κ²½μš°λ„ 많음

νŒŒμ΄μ¬μ€‘μ–΄λ–»κ²Œ ν•˜λ©΄ μ‰½κ²Œ μ½”λ”©ν•  수 μžˆμ„κΉŒ’에 λŒ€ν•œ κ³ λ―Ό 끝에 λ‚˜μ˜¨ μ–Έμ–΄

μƒˆλ‘œμš΄ λ°©λ²•μœΌλ‘œ μ½”λ”©ν•˜λŠ” 것보닀 ν•„μš”ν•œ 방법을 μ μž¬μ μ†Œμ— 적용 μ‹œμΌœ μ†μ‰½κ²Œ 문제λ₯Ό ν•΄κ²°ν•΄μ•Ό 함


객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ κΈ°λ³Έ μ² ν•™ : μ‹€μƒν™œμ„ λͺ¨λΈλ§

ex) 상속, λ‹€ν˜•μ„±, κ°€μ‹œμ„±

상속

  • 무엇인가λ₯Ό λ‚΄λ €λ°›λŠ” 것
  • λΆ€λͺ¨ ν΄λž˜μŠ€μ— μ •μ˜λœ 속성과 λ©”μ„œλ“œλ₯Ό μžμ‹ ν΄λž˜μŠ€κ°€ λ¬Όλ €λ°›μ•„ μ‚¬μš©ν•˜λŠ” 것
>>> class Person(object):
        pass

objectκ°€ Person 클래슀의 λΆ€λͺ¨ν΄λž˜μŠ€

object : νŒŒμ΄μ¬μ—μ„œ μ‚¬μš©ν•˜λŠ” κ°€μž₯ κΈ°λ³Έ 객체

파이썬 μ–Έμ–΄κ°€ 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ΄λ―€λ‘œ λͺ¨λ“  λ³€μˆ˜λŠ” κ°μ²΄μž„

>>> a = "abc"
>>> type(a)
<class 'str'>

μ§€κΈˆκΉŒμ§€ λ¬Έμžμ—΄ν˜•μ΄λΌκ³  λΆˆλ €μ§€λ§Œ λ‚΄λΆ€μ μœΌλ‘œλŠ” 객체둜 μ²˜λ¦¬λ˜μ–΄μžˆμŒ

파이썬의 λͺ¨λ“  κ°μ²΄λŠ” object 객체λ₯Ό 상속함

>>> class Person(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age
>>>
>>> class Korean(Person):          # κ°„λ‹¨νžˆ 상속받기
        pass                       # λ³„λ„μ˜ λ‚΄μš© 없이 클래슀만 쑴재
>>>
>>> first_korean = Korean("Sungchul", 35)
>>> print(first_korean.name)
Sungchul

Person 클래슀λ₯Ό λ¨Όμ € 생성

Korean 클래슀λ₯Ό λ§Œλ“€λ©΄μ„œ Person 클래슀λ₯Ό μƒμ†λ°›μŒ

Korean ν΄λž˜μŠ€λŠ” λ³„λ„μ˜ μƒμ„±μžλŠ” μ—†μ§€λ§Œ, Person ν΄λž˜μŠ€κ°€ 가진 μƒμ„±μžλ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ—¬ μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“¦

  • μ‚¬κ°ν˜•μ΄ 클래슀, ν™”μ‚΄ν‘œλŠ” 각 클래슀의 상속 관계
  • 상속을 ν•˜κ²Œ 되면 λΆ€λͺ¨ ν΄λž˜μŠ€λ³΄λ‹€ μžμ‹ 클래슀의 정보가 더 ꡬ체화 됨
  • λΆ€λͺ¨ κ°μ²΄μ—λŠ” 일반적 κΈ°λŠ₯을, μžμ‹ κ°μ²΄μ—λŠ” μƒμ„Έν•œ κΈ°λŠ₯을 λ„£μ–΄μ•Ό 함
  • λΆ€λͺ¨ 객체보닀 μžμ‹ 객체에 μ’€ 더 λ§Žμ€ 정보λ₯Ό 쀌 (λΆ€λͺ¨ 클래슀의 λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜)
>>> class Person(object):                        # λΆ€λͺ¨ 클래슀 Person μ„ μ–Έ
        def __init__(self, name, age, gender):
            self.name = name
            self.age = age
            self.gender = gender

        def about_me(self):                      # λ©”μ„œλ“œ μ„ μ–Έ
            print("μ €μ˜ 이름은", self.name, "μ΄κ³ μš”, 제 λ‚˜μ΄λŠ”", str(self.age), "μ‚΄μž…λ‹ˆλ‹€.")
>>>
>>>
>>> class Employee(Person):                      # λΆ€λͺ¨ 클래슀 PersonμœΌλ‘œλΆ€ν„° 상속
        def __init__(self, name, age, gender, salary, hire_date):
            super().__init__(name, age, gender)  # λΆ€λͺ¨ 객체 μ‚¬μš©
            self.salary = salary
            self.hire_date = hire_date           # 속성값 μΆ”κ°€

        def do_sork(self):                       # μƒˆλ‘œμš΄ λ©”μ„œλ“œ μΆ”κ°€
            print("μ—΄μ‹¬νžˆ 일을 ν•œλ‹€.")

        def about_me(self):                      # λΆ€λͺ¨ 클래슀 ν•¨μˆ˜ μž¬μ •μ˜
            super().about_me()                   # λΆ€λͺ¨ 클래슀 ν•¨μˆ˜ μ‚¬μš©
            print("제 κΈ‰μ—¬λŠ”", self.salary, "원이고, 제 μž…μ‚¬μΌμ€", self.hire_date, "μž…λ‹ˆλ‹€.")

Person 클래슀 → λ‹¨μˆœνžˆ μ‚¬λžŒμ— λŒ€ν•œ 정보 μ •μ˜

Employee 클래슀 → μΌν•˜λŠ” μ‹œκ°„κ³Ό 월급에 λŒ€ν•œ λ³€μˆ˜ μΆ”κ°€ (init()ν•¨μˆ˜ μž¬μ •μ˜)

  • μ˜€λ²„λΌμ΄λ”© : 상속 μ‹œ ν•¨μˆ˜ 이름과 ν•„μš”ν•œ λ§€κ°œλ³€μˆ˜λŠ” κ·ΈλŒ€λ‘œ μœ μ§€ν•˜λ©΄μ„œ ν•¨μˆ˜μ˜ μˆ˜ν–‰ μ½”λ“œλ₯Ό λ³€κ²½ν•˜λŠ” 것
    • super().init(λ§€κ°œλ³€μˆ˜) : λΆ€λͺ¨ 클래슀의 init() ν•¨μˆ˜λ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©
    • κΈ°μ‘΄ ν•¨μˆ˜λ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜λ €λ©΄ λ³„λ„μ˜ init() ν•¨μˆ˜λ₯Ό λ§Œλ“€μ§€ μ•Šμ•„λ„ 됨
    • But, κΈ°μ‘΄ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄μ„œ μƒˆλ‘œμš΄ λ‚΄μš©μ„ μΆ”κ°€ν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©

μžμ‹ ν΄λž˜μŠ€μ—λ§Œ ν•„μš”ν•œ μƒˆλ‘œμš΄ ν•¨μˆ˜λ₯Ό 생성할 수 있음

λ‹€μ–‘ν•œ 상속 κΈ°λŠ₯ 지원 → ex) 닀쀑상속 : 1개 μ΄μƒμ˜ 클래슀 νŠΉμ§•μ„ 상속


λ‹€ν˜•μ„±

  • 같은 μ΄λ¦„μ˜ λ©”μ„œλ“œκ°€ λ‹€λ₯Έ κΈ°λŠ₯을 ν•˜λŠ” 것
  • ex) λΆ€λͺ¨ ν΄λž˜μŠ€μ™€ μžμ‹ ν΄λž˜μŠ€κ°€ μ„œλ‘œ λ‹€λ₯Έ about_me ν•¨μˆ˜ κ΅¬ν˜„
>>> n_crawler = NaverCrawler()
>>> d_crawler = DaumCrawler()
>>> cralwers = [n_crawler, d_crawler]
>>> news = []
>>> for cralwer in cralwers:
        news.append (eralwer.do_crawling())

λΆ€λͺ¨ Crawler 클래슀 → do_crawling ν•¨μˆ˜ 생성

Crawler 클래슀λ₯Ό μƒμ†μ‹œμΌœ NaverCrawler와 DaumCrawlerλ₯Ό λ§Œλ“¦

  • 두 μžμ‹ ν΄λž˜μŠ€λŠ” λͺ¨λ‘ do_crawling ν•¨μˆ˜λ₯Ό κ°–κ²Œ 됨. But, κ΅¬ν˜„ λ‚΄μš©μ€ 닀름
  • 각 ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„λ˜λŠ” λ‚΄λΆ€ λ‘œμ§μ— 차이가 μ‘΄μž¬ν•˜λŠ”λ° 이λ₯Ό ν•¨μˆ˜μ˜ λ‹€ν˜•μ„±μ΄λΌκ³  함
  • μ‚¬μš©μžλŠ” ν•¨μˆ˜μ˜ μ΄λ¦„λ§Œ μ•Œλ©΄ 같은 ν˜•νƒœλ‘œ μ‚¬μš© κ°€λŠ₯ν•˜μ§€λ§Œ, 개발자의 μž…μž₯μ—μ„œλŠ” 내뢀적인 κ΅¬ν˜„μ„ 각 클래슀 λ³„λ‘œ λ‹€λ₯΄κ²Œ κ°œλ°œν•  ν•„μš”κ°€ 있음
  • λ‹€ν˜•μ„±μ„ μ‚¬μš©ν•˜λ©΄ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•  λ•Œ μ‚¬μš©μžκ°€ μ’€ 더 μ‰½κ²Œ 클래슀λ₯Ό μ‚¬μš© κ°€λŠ₯
>>> class Animal:
        def __init__(self, naem):
            self.name = name
        def talk(self):
            raise NotImplementedError("Subclass must implement abstract method")

>>> class Cat(Animal):
        def talk(self):
            return 'Meow!'

>>> class Dog(Animal):
        def talk(self):
            return 'Woof! Woof!'

>>> animals = [Cat('Missy'), Cat('Mr.Mistoffelees'), Dog('Lassie')]
>>> for animal in animals:
        print(animal.name + ': ' +animal.talk())
Missy: Meow!
Mr.Mistoffelees: Meow!
Lassie: Woof! Woof!

λΆ€λͺ¨ ν΄λž˜μŠ€λŠ” Animal이며, Catκ³Ό DogλŠ” Animal 클래슀λ₯Ό μƒμ†λ°›μŒ

핡심 ν•¨μˆ˜λŠ” talk으둜, 두 동물 클래슀의 역할이 λ‹€λ₯Έ 것을 확인할 수 있음

  • NotImplementedError : μžμ‹ ν΄λž˜μŠ€μ—λ§Œ ν•΄λ‹Ή ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλ„λ‘

κ°€μ‹œμ„±

  • 객체의 정보λ₯Ό λ³Ό 수 μžˆλŠ” λ ˆλ²¨μ„ μ‘°μ ˆν•˜μ—¬ 객체의 정보 접근을 μˆ¨κΈ°λŠ” 것
  • μΊ‘μŠν™” : 객체의 λ§€κ°œλ³€μˆ˜ μΈν„°νŽ˜μ΄μŠ€λ§Œ λͺ…ν™•νžˆ μ•Œλ©΄ μ‚¬μš© κ°€λŠ₯
  • 정보 은닉 : ν•„μš”ν•œ μ •λ³΄λŠ” μˆ¨κ²¨μ•Ό 함

→ μ™ΈλΆ€μ—μ„œ μ‰½κ²Œ μ‚¬μš© κ°€λŠ₯ν•˜κ³ , μ½”λ“œμ˜ 세뢀적인 λ‚΄μš©μ€ λͺ¨λ₯΄κ²Œ ν•œλ‹€λŠ” μΈ‘λ©΄μ—μ„œ λΉ„μŠ·ν•œ 의미둜 μ‚¬μš©λ¨

μΊ‘μŠν™”λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” μ΄μœ λŠ” λ¬΄μ—‡μΌκΉŒ?

  • 클래슀 κ°„ κ°„μ„­ 및 정보 곡유λ₯Ό μ΅œμ†Œν™”ν•˜μ—¬ κ°œλ³„ ν΄λž˜μŠ€κ°€ λ‹¨λ…μœΌλ‘œλ„ 잘 λ™μž‘ν•  수 μžˆλ„λ‘ ν•΄μ•Όν•˜κΈ° λ•Œλ¬Έ
  • 각 ν΄λž˜μŠ€κ°€ κ°•ν•˜κ²Œ μ—°κ²°λ˜μ–΄ μžˆλ‹€λ©΄ λ…λ¦½μ μœΌλ‘œ μ‚¬μš©ν•˜κΈ° 어렀움
  • μ‚¬μš©μž μž…μž₯μ—μ„œλŠ” μƒμ„Έν•œ λ‚΄μš©μ„ λͺ¨λ₯΄λ”라도 μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ΄ν•΄ν•˜λ©΄ 클래슀λ₯Ό μ‰½κ²Œ μ‚¬μš©ν•  수 있음

νŒŒμ΄μ¬μ—μ„œλŠ” μ΄λŸ¬ν•œ κ°œλ…μ„ κ°€μ‹œμ„±μ΄λΌλŠ” μ΄λ¦„μœΌλ‘œ μ μš©μ‹œν‚€κ³ . 정보 은닉을 μ–΄λ–»κ²Œ ν•  것인가λ₯Ό μ½”λ“œ λ ˆλ²¨μ—μ„œ μ‘°μ ˆν•¨

❓ Product λ₯Ό Inventory 객체에 μΆ”κ°€

Inventoryμ—λŠ” 였직 Product 객체만 듀어감

Inventory에 ProductκΈ° λͺ‡ κ°œμΈμ§€ 확인이 ν•„μš”ν•¨

Inventory에 Product itemsλŠ” 직접 접근이 λΆˆκ°€ν•¨

 

>>> class Prodcut(object):
        pass
>>>
>>> class Inventory(object):
        def __init__(self):
            self.__items = []
        def add_new_item(self, product):
            if type(prodcut) == Product:
                self.__items.append(prodct)
                print("new item added")
            else:
                raise ValueError("Invalid Item")
        def get_number_of_items(self):
            return len(self.__items)

>>> my_inventory = Inventory()
>>> my_inventory.add_new_item(Prodcut())
>>> my_inventroy.add_new_item(Product())
>>>
>>> my_inventory.__items
new item added
new item added
Traceback (most recent call last):
    File "visibilityl.py", line 20, in <modul>
    my_inventory_iterns
AttributeError: 'Inventory' object has no attribute '__items'

Inventory 객체에 add_new_item() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ μƒˆλ‘­κ²Œ μƒμ„±λœ Product 객체λ₯Ό λ„£μ–΄μ€Œ

μžλ£Œν˜•μ„ λΉ„κ΅ν•˜μ—¬ ν•΄λ‹Ή 객체가 Product 객체인지 ν™•μΈν•˜κ³ , 그렇지 μ•Šμ„ 경우 ValueErrorκ°€ λ°œμƒ

itemsλŠ” Product 객체가 λ“€μ–΄κ°€λŠ” κ³΅κ°„μœΌλ‘œ, get_number_of_items()λ₯Ό μ‚¬μš©ν•˜μ—¬ 총 객체의 개수λ₯Ό λ°˜ν™˜

  • __items λ³€μˆ˜ : Inventoryλ₯Ό μ €μž₯ν•˜λŠ” κ³΅κ°„μœΌλ‘œ, add_new_item()을 톡해 Product 객체λ₯Ό 넣을 수 있음
  • add_new_item()이 μ•„λ‹ˆλΌ 직접 ν•΄λ‹Ή 객체에 μ ‘κ·Όν•΄ μƒˆλ‘œμš΄ 값을 μΆ”κ°€ν•˜λ €κ³  ν•œλ‹€λ©΄ 였λ₯˜ λ°œμƒν΄λž˜μŠ€ λ‚΄λΆ€μš©μœΌλ‘œλ§Œ λ³€μˆ˜λ₯Ό μ‚¬μš© → ‘__λ³€μˆ˜λͺ…’
  • κ°€μ‹œμ„±μ„ 클래슀 λ‚΄λ‘œ ν•œμ •ν•˜λ©΄μ„œ 값이 λ‹€λ₯΄κ²Œ λ“€μ–΄κ°€λŠ” 것을 막을 수 있음(정보 은닉)
  • _(특수 역할을 ν•˜λŠ” μ˜ˆμ•½ 문자)κ°€ ν΄λž˜μŠ€μ—μ„œ λ³€μˆ˜μ— 두 κ°œκ°€ λΆ™μ–΄μžˆμ–΄ μ‚¬μš©λ  클래슀 λ‚΄λΆ€μ—μ„œλ§Œ μ ‘κ·Όν•  수 있고, μ™ΈλΆ€μ—λŠ” ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©ν•˜μ§€ λͺ»ν•˜κΈ° λ•Œλ¬Έ

μ΄λŸ¬ν•œ 정보λ₯Ό 클래슀 μ™ΈλΆ€μ—μ„œ μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ”?

  • @property μ‚¬μš©
  • λ°μ½”λ ˆμ΄ν„° : 클래슀의 각 λ©”μ„œλ“œ 상단에 μ‚½μž…ν•˜μ—¬ ν•΄λ‹Ή λ©”μ„œλ“œμ˜ κΈ°λŠ₯을 μΆ”κ°€ν•˜λŠ” 파이썬 문법
>>> class Inventory(object):
        def __init__(self):
            self.__items = []      # privae λ³€μˆ˜λ‘œ μ„ μ–Έ (타인이 μ ‘κ·Ό λͺ» 함)

        @property                  # property λ°μ½”λ ˆμ΄ν„° (μˆ¨κ²¨μ§„ λ³€μˆ˜ λ°˜ν™˜)
        def items(self):
            return self.__items

>>> my_inventory = Inventory()
>>> items = my_inventory.items
>>> items.append(Product())

λ‹€λ₯Έ μ½”λ“œλŠ” κ·ΈλŒ€λ‘œ μœ μ§€ν•˜κ³  λ§ˆμ§€λ§‰μ— itemsλΌλŠ” μ΄λ¦„μœΌλ‘œ λ©”μ„œλ“œλ₯Ό λ§Œλ“€λ©΄μ„œ @property λ₯Ό λ©”μ„œλ“œ 상단에 μž…λ ₯

μ™ΈλΆ€μ—μ„œ μ‚¬μš©ν•  λ³€μˆ˜μΈ __itemsλ₯Ό λ°˜ν™˜

** __items λ³€μˆ˜μ˜ μ›λž˜ 이름이 μ•„λ‹Œ items둜 호좜

@propertyλ₯Ό 뢙인 ν•¨μˆ˜ μ΄λ¦„μœΌλ‘œ μ‹€μ œ __itemsλ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” 것이

μ΄λŠ” κΈ°μ‘΄ private λ³€μˆ˜λ₯Ό λˆ„κ΅¬λ‚˜ μ‚¬μš©ν•  수 μžˆλŠ” public λ³€μˆ˜λ‘œ λ°”κΎΈλŠ” 방법 쀑 ν•˜λ‚˜