α„‚ πŸ˜„ [10 일차] : FUNDAMENTAL 13. λ‚˜λ§Œμ˜ n면체 μ£Όμ‚¬μœ„ λ§Œλ“€κΈ°

백건·2022λ…„ 1μ›” 21일
0
----
layout: single
title: Fundamuntal 13. λ‚˜λ§Œμ˜ n면체 μ£Όμ‚¬μœ„ λ§Œλ“€κΈ°
----

λ‚˜λ§Œμ˜ n면체 μ£Όμ‚¬μœ„ μœ„μ ― λ§Œλ“€κΈ°

  • 파이썬 클래슀 문법 읡히자
  • 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ— λŒ€ν•΄ 배우자

κ°μ²΄λž€?

파이썬(Python)μ—μ„œ λͺ¨λ“  것은 객체(object)λ‹€. 그리고 λŒ€λΆ€λΆ„μ˜ κ°μ²΄λŠ” 속성(attributes)κ³Ό λ©”μ„œλ“œ(methods)λ₯Ό κ°–λŠ”λ‹€
Everything is an Object
λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•˜λŠ” λ‚΄μž₯ 속성을 docλ₯Ό 가지고 μžˆλ‹€.

λ¨Όμ € λ³€μˆ˜λ₯Ό μ΄μ•ΌκΈ°ν•΄λ³΄μž. μ™œ????

myvar = 3
myvar
3

λ³€μˆ˜λΌλŠ” 이름을 λΆ™μ΄λŠ” 것.

  • μ§„μ§œ 객체λ₯Ό λ³€μˆ˜λͺ…μœΌλ‘œ κ°€λ¦¬μΌœ μ°Έμ‘°ν•  수 μžˆλ„λ‘ ν•˜λŠ” 것.
    • κΈΈλ©΄ 짧게 ν‘œμ‹œν•  수 μžˆλ‹€λŠ” 거지.
    • 이름이 κΈΈλ©΄ λΆ€λ₯΄κΈ° νž˜λ“œλ‹ˆκΉŒ
myname = 'λ°•ν•˜λŠ˜λ³„λ‹˜κ΅¬λ¦„ν–‡λ‹˜λ³΄λ‹€μ‚¬λž‘μŠ€λŸ¬μš°λ¦¬μ”¨'
myname
'λ°•ν•˜λŠ˜λ³„λ‹˜κ΅¬λ¦„ν–‡λ‹˜λ³΄λ‹€μ‚¬λž‘μŠ€λŸ¬μš°λ¦¬μ”¨'

μœ„ 이름은 ν•œκ΅­μ—μ„œ κ°€μž₯ κΈ΄ 이름인데 μ €κ±Έ mynameμ΄λΌλŠ” 걸둜 μ€„μ—¬λ²„λ¦¬λŠ” 것.

λͺ¨λ“  λ¬Έμžμ—΄ ν΄λž˜μŠ€μ—λŠ” upper(0) μ΄λΌλŠ” λ©”μ†Œλ“œκ°€ μžˆλ‹€.

myword = 'cat'
myword
'cat'
myword.upper()
'CAT'
'cat'.upper()
'CAT'

myword이 κ°€λ¦¬ν‚€λŠ” λ¬Έμžμ—΄κ³Ό 'cat'μ΄λΌλŠ” 값이 κ°™λ‹€

id()λΌλŠ” ν•¨μˆ˜

  • 객체의 identityλ₯Ό ν™•μΈν•˜λŠ” λ‚΄μž₯ ν•¨μˆ˜
  • 각 객체의 κ³ μœ κ°’
  • λ©”λͺ¨λ¦¬μ— λΆ€μ—¬λœ μœ μΌν•œ κ°’
  • λ©”λͺ¨λ¦¬ μ£Όμ†ŒλΌκ³  함.
var = 4
var
4
print(id(var), id(4)) #id 값은 pcλ§ˆλ‹€ λ‹€λ¦…λ‹ˆλ‹€.
4482629968 4482629968

얕은 볡사, κΉŠμ€ 볡사

copy DeepCopoy

    • μ–΄λ–€ κ°’(4)이 있고 이것에 이름(var)을 λΆ™μ—¬μ€€λ‹€.
    • μ–΄λ–€ 값은 고유의 μ£Όμ†Œ(4482629968)λ₯Ό 가지고 μžˆλ‹€.
    • μ–΄λ–€ 이름(var)이 κ°’(4)을 κ°€λ₯΄ν‚€λŠ” 것을 μ°Έμ‘°ν•œλ‹€κ³  ν•œλ‹€.
    • mutable 은 μ£Όμ†Œκ°’μ΄ κ·ΈλŒ€λ‘œμΈμ±„ 값이 λ³€ν•œλ‹€. (원본 μžμ²΄κ°€ λ°”λ€λ‹€λŠ” λœ»μ΄λ‹€)
      - list(μˆœμ„œκ°€ μžˆλ‹€), set(μˆœμ„œκ°€ μ—†λ‹€), dict(μˆœμ„œ 없이, key, valueκ°€ μžˆλ‹€)
    • Immutable 은 값이 λ°”λ€Œλ©΄ μ£Όμ†Œκ°’λ„ 바뀐닀. (원본이 λ‚¨μ•„μžˆλ‹€λŠ” λœ»μ΄λ‹€)
      - bool, int, float, tuple, str, frozenset(Immutable Set)
    • copy (shallow copy)
      • 객체의 μ£Όμ†Œλ₯Ό 볡사
    • deepcopy : λΆ•μ–΄λΉ΅ 기계 같은거 : μƒˆλ‘œμš΄ κ±Έ 찍어낸닀.
      • 객체의 값을 볡사
import copy
a = [1,2,3,4]
b = copy.deepcopy(a)
print(a,b)
print(id(a), id(b))
b[1] = 0
print(a,b)
print(id(a), id(b))
[1, 2, 3, 4] [1, 2, 3, 4]
4544586880 4544587712
[1, 2, 3, 4] [1, 0, 3, 4]
4544586880 4544587712
import copy
c = [1,2,3,4]
d = copy.copy(a)
print(c,d)
print(id(c), id(d))
d[1] = 0
print(c,d)
print(id(c), id(d))
[1, 2, 3, 4] [1, 2, 3, 4]
4544587776 4544767104
[1, 2, 3, 4] [1, 0, 3, 4]
4544587776 4544767104
import copy
e = [1,2,3,4]
f = a.copy()
print(e,f)
print(id(e), id(f))
f[1] = 0
print(e,f)
print(id(e), id(f))
[1, 2, 3, 4] [1, 2, 3, 4]
4544585280 4526864000
[1, 2, 3, 4] [1, 0, 3, 4]
4544585280 4526864000
# λ³€μˆ˜ var에 리슀트 λ³€μˆ˜ mylist λ₯Ό ν• λ‹Ή.
mylist = [1,2,3]
var = mylist
var
[1, 2, 3]
# mylist에 append()λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ μ›μ†Œ 4 μΆ”κ°€
mylist.append(4)
print(mylist)
[1, 2, 3, 4]

객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ΄λž€

  • λ³€μˆ˜(a_tire, b_tire,...)κ°€ λ„ˆλ¬΄ λ§Žμ•„μ§€κ³ , ν•¨μˆ˜{a_run(), b_run(),....) κ°€ λ§Žμ•„μ Έμ„œ λ³΅μž‘ν•¨
  • ν•˜λ‚˜λ‘œ λ¬Άμ–΄μ„œ ν‘œν˜„ -> 클래슀 car -> ν΄λž˜μŠ€μ— 묢인 객체 car_a_tire / ν•¨μˆ˜ car_a_run()

클래슀 μ„ μ–Έ 및 μΈμŠ€ν„΄μŠ€ν™”

클래슀 μ„ μ–Έν•˜κΈ° class

fu13-01.png

class Car:
    pass

class Car():
    pass

#id(Car)λŠ” μ—¬λŸ¬λ²ˆ ν˜ΈμΆœν•΄λ„ 같은 값이 μ–»μ–΄μ§‘λ‹ˆλ‹€. 
print(id(Car))
print(id(Car))

#id(Car())λŠ” Car()κ°€ 호좜될 λ•Œλ§ˆλ‹€ λ‹€λ₯Έ 값이 μ–»μ–΄μ§‘λ‹ˆλ‹€. 
print(id(Car()))
print(id(Car()))

# 두 객체의 type을 μ‚΄νŽ΄λ΄…λ‹ˆλ‹€. 
print(type(Car))
print(type(Car()))
140582709239360
140582709239360
4526845680
4526834832
<class 'type'>
<class '__main__.Car'>

car()λŠ” typeμœ ν˜•μ˜ 객체
id(car())은 main.car μœ ν˜• ????
car의 idλŠ” κ³ μ • / car()의 idλŠ” ν˜ΈμΆœν• λ•Œ λ§ˆλ‹€ 생성
car λŠ” κ·ΈλŒ€λ‘œ car μ΄μ§€λ§Œ car()λŠ” ν˜ΈμΆœν• λ•Œλ§ˆλ‹€ μƒˆλ‘œμš΄ car νƒ€μž…μ˜ 객체가 생성
id(car())은 ν˜ΈμΆœλ λ•Œλ§ˆλ‹€ λ‹€λ₯Έ id

클래슀 μ‚¬μš© - 객체 μΈμŠ€ν„΄μŠ€ν™”

  • 클래슀λ₯Ό μ΄μš©ν•˜κΈ° μœ„ν•΄ 객체둜 λ§Œλ“€μ–΄μ€˜μ•Ό ν•œλ‹€
  • -> μΈμŠ€ν„΄μŠ€ν™”
  • μ„ μ–Έν•œ ν΄λž˜μŠ€μ— ()λ₯Ό λΆ™μ—¬μ€€λ‹€.
class Car:
    pass

mycar = Car()
mycar2 = Car()
print(id(mycar))
print(id(mycar2))
4526846112
4526845152
1. 클래슀λͺ…에 κ΄„ν˜Έλ₯Ό μΆ”κ°€ν•΄ 객체둜
2. mycar 와 mycar2λΌλŠ” λ³€μˆ˜μ— Car 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό ν• λ‹Ή -> 클래슀λ₯Ό callν–ˆλ‹€.
  • Car 클래슀λ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ mycarλΌλŠ” μΈμŠ€ν„΄μŠ€λ₯Ό μ‚¬μš©
  • mycar μΈμŠ€ν„΄μŠ€λ₯Ό μ΄μš©ν•΄ Car 클래슀 μ‚¬μš©
  • Car μ•ˆμ˜ λ‹€μ–‘ν•œ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄

ν‘œκΈ°λ²•

  1. 클래슀 각 λ‹¨μ–΄μ˜ μ•ž κΈ€μžλ₯Ό λŒ€λ¬Έμžλ‘œ μ“Έ 것.
    • mycar -> MyCar
    • 주둜 λͺ…μ‚¬λ‘œ
  2. ν•¨μˆ˜ λ‹¨μ–΄λŠ” μ†Œλ¬Έμžλ‘œ μ“°κ³  각 λ‹¨μ–΄μ˜ 연결은 μ–Έλ”λ°”λ‘œ 할것
    • mycar -> my_car
      ν•¨μˆ˜λͺ…은 주둜 λ™μ‚¬λ‘œ

클래슀 속성과 λ©”μ†Œλ“œ

클래슀의 속성과 λ©”μ†Œλ“œ

  • 클래슀 속상은 μƒνƒœ(state)λ₯Ό ν‘œν˜„
  • 속성은 λ³€μˆ˜λ‘œ λ‚˜νƒ€λƒ„
  • λ©”μ†Œλ“œλŠ” λ™μž‘(behavior)을 ν‘œν˜„
  • λ©”μ†Œλ“œλŠ” def ν‚€μ›Œλ“œλ‘œ λ‚˜νƒ€λƒ„ -> μ–΄λ–»κ²Œ λ™μž‘ν•  지 μ •μ˜λ₯Ό ν•΄μ€˜μ•Ό 함.

Car 클래슀λ₯Ό μ½”λ”©ν•΄λ³΄μž

\색상(color)\ 은 "red" \μ’…λ₯˜(category)\λŠ” "sports car" λΌλŠ” 속성을 가지고
\μ£Όν–‰\κ³Ό \가속\μ΄λΌλŠ” λ™μž‘ 을 κ°–λŠ” 클래슀

  • μ—¬κΈ°μ„œ 속성은 color, category
    • color = 'red'
    • category = 'sports car'
  • λ™μž‘μ€ λ©”μ†Œλ“œ -> defλ₯Ό μ‚¬μš©
    • def driving():
      pass
      def accel():
      pass
class Car:
    color = 'red'
    category = 'sports car'

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
# Car ν΄λž˜μŠ€μž…λ‹ˆλ‹€.
class Car:
    '''
    속성은 클래슀의 μƒνƒœλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
    색상: λΉ¨κ°•
    μ’…λ₯˜: 슀포츠 μΉ΄
    '''
    color = 'red'
    category = 'sports car'
		
    '''
    λ™μž‘μ€ λ©”μ„œλ“œλ‘œ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

    

        μ£Όν–‰ λ©”μ„œλ“œ
        '''
    def drive(self):
        '''
        μž‘λ™ λ‚΄μš©
        '''
        print("I'm driving")
        '''
        가속 λ©”μ„œλ“œ
        :param speed_up: ν˜„μž¬μ†λ„    / :type speed_up: string
        :param current_speed: 가속  / :type current_speed: string      

        '''		
    def accel(self, speed_up, current_speed=10):
        '''   
        '''
        self.speed_up = speed_up
        self.current_speed = current_speed + speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
속성
  • 클래슀의 μƒνƒœλŠ” 속성이라고 λΆ€λ₯΄λ©°, λ³€μˆ˜λ‘œ μ„ μ–Έν•©λ‹ˆλ‹€.
  • colorμ—λŠ” 'red'λ₯Ό categoryμ—λŠ” 'sports car'λ₯Ό ν• λ‹Ήν•©λ‹ˆλ‹€.
λ©”μ†Œλ“œ
  • λ™μž‘μ€ λ©”μ„œλ“œλ‘œ λ‚˜νƒ€λ‚΄κ³  λ©”μ„œλ“œ 선언은 ν•¨μˆ˜μ™€ λ˜‘κ°™μ΄ def ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•©λ‹ˆλ‹€.
  • 클래슀의 λ©”μ„œλ“œλŠ” 첫 번째 μΈμžλŠ” self 값을 적어 μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€. (self 에 λŒ€ν•œ μ„€λͺ…은 λ‚˜μ€‘μ— ν•˜κ² μŠ΅λ‹ˆλ‹€)
  • 주행은 drive() λΌλŠ” λ©”μ„œλ“œλͺ…μœΌλ‘œ μ •μ˜ν•˜κ³  self외에 μΈμžλŠ” μ—†μŠ΅λ‹ˆλ‹€.
  • 가속은 accel() λΌλŠ” λ©”μ„œλ“œλͺ…μœΌλ‘œ μ •μ˜ν•˜κ³  ν˜„μž¬ 속도와 가속할 값을 인자둜 λ°›μŠ΅λ‹ˆλ‹€.
    • ν˜„μž¬μ†λ„ 인자λͺ… = current_speed, 가속 인자λͺ… = speed_up
    • accel λ©”μ„œλ“œμ— self.speed_up, self.current_speed λ₯Ό 인자둜 μ€λ‹ˆλ‹€.

클래슀의 속성과 λ©”μ†Œλ“œ μ‚¬μš©

속성. :

μΈμŠ€ν„΄μŠ€λͺ… = 클래슀λͺ…() / mycar = Car()

μΈμŠ€ν„΄μŠ€.속성λͺ… / mycar.color

  • μΈμŠ€ν„΄μŠ€.속성λͺ…
mycar = Car() # μΈμŠ€ν„΄μŠ€λͺ… = 클래슀λͺ…()
print(mycar.color) # μΈμŠ€ν„΄μŠ€λͺ….속성λͺ…
red
mycar.price
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/4236691067.py in <module>
----> 1 mycar.price


AttributeError: 'Car' object has no attribute 'price'

μ—†λŠ” 속성값에 μ ‘κ·Όν•˜λ©΄ μ—λŸ¬κ°€ λ°œμƒ

λ©”μ†Œλ“œ

  • λ©”μ„œλ“œ 호좜 (feat. μΈν„°ν”„λ¦¬ν„°μ—μ„œ λ²Œμ–΄μ§€λŠ” 일)
  • μΈμŠ€ν„΄μŠ€.λ©”μ†Œλ“œλͺ…() / λ©”μ†Œλ“œλŠ” ν•¨μˆ˜μΈκ±°.
    • ν•¨μˆ˜μ—λŠ” μΈμžμ™€ 값이 μžˆμ–΄μ•Όμ§€.-> μΈμžκ°€ μ—†μœΌλ©΄ κ·Έλƒ₯ 지가 μžˆλŠ” κ±°.
#  mycar.drive() μ½”λ“œλŠ” 인터프리터 λ‚΄λΆ€μ—μ„œλŠ” Car.drive(mycar)둜 λ™μž‘

mycar.drive() # carλΌλŠ” 클래슀λ₯Ό mycar λΌλŠ” μΈμŠ€ν„΄μŠ€κ°€ drive λΌλŠ” λ©”μ†Œλ“œλ‘œ λ™μž‘

# μ΄λ ‡κ²Œ μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” mycar μΈμŠ€ν„΄μŠ€κ°€ Car의 ν΄λž˜μŠ€μž„μ„ λ¨Όμ € μ„ μ–Έ
I'm driving
mycar.accel(5)
speed up 5 driving at 15

mycar.drive() μ½”λ“œλŠ” 인터프리터 λ‚΄λΆ€μ—μ„œλŠ” Car.drive(mycar)둜 λ™μž‘



fu13-02.png


이것이 μž‘λ™ν•  수 μžˆλ„λ‘ ν•˜λŠ” 것이 self

fu13-03.png

class Test:
    def run1(self):
        print("run1")

    def run2():
        print("run2")

t = Test()
# μ΄λ ‡κ²ŒλŠ” 잘 λ™μž‘ν•˜μ§€λ§Œ
t.run1()
run1
# μ΄λ ‡κ²ŒλŠ” λ™μž‘ν•˜μ§€ μ•ŠμŒ
t.run2()
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/1561068890.py in <module>
      1 # μ΄λ ‡κ²ŒλŠ” λ™μž‘ν•˜μ§€ μ•ŠμŒ
----> 2 t.run2()


TypeError: Test.run2() takes 0 positional arguments but 1 was given

접두사 self

  • μΈμŠ€ν„΄μŠ€ μ†μ„±μœΌλ‘œ μ‚¬μš©ν•˜κ³  싢은 λ³€μˆ˜λŠ” self.λ₯Ό μ‚¬μš©
  • self 인자λ₯Ό 톡해 μ„ μ–Έλœ 객체의 κ°’μ΄λž€ 의미
class Test2:
    def run1(self, a):
        self.a = float(a) * 10
        print(self.a)

    def run2(self, b):
        b = float(b) + 10
        print(self.b)
        
t = Test2()
t.run1(1)
10.0
t.run2(1) # self λ₯Ό μ΄μš©ν•˜μ—¬ μ°Έμ‘°ν•˜μ§€ μ•Šμ•˜κΈ° 떄문에 λ°–μ—μ„œλŠ” μ•ˆλ¨
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/25985708.py in <module>
----> 1 t.run2(1)


/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/3234681881.py in run2(self, b)
      6     def run2(self, b):
      7         b = float(b) + 10
----> 8         print(self.b)
      9 
     10 t = Test2()


AttributeError: 'Test2' object has no attribute 'b'
class Test2:
    def run1(self, a):
        self.a = float(a) * 10
        print(self.a)

    def run2(self, b):
        b = float(b) + 10
        print(b)           #μš”κΈ°μ„œ selfλ₯Ό μ—†μ• κ³  좜λ ₯
        
t = Test2()
t.run2(1)
11.0
class Test2:
    def run1(self, a):
        self.a = float(a) * 10
        print(self.a)

    def run2(b):
        b = float(b) + 10
        print(b)
        
t = Test2()
t.run2(1)
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/4213503736.py in <module>
      9 
     10 t = Test2()
---> 11 t.run2(b)


TypeError: Test2.run2() takes 1 positional argument but 2 were given
  • selfλŠ” 자기 μžμ‹ μž…λ‹ˆλ‹€.
  • ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ 객체(μΈμŠ€ν„΄μŠ€)λ₯Ό κ°€λ¦¬ν‚΅λ‹ˆλ‹€.
  • 클래슀의 λ©”μ„œλ“œλŠ” 인자둜 ν•΄λ‹Ή μΈμŠ€ν„΄μŠ€(self)λ₯Ό λ°›μ•„μ•Ό ν•©λ‹ˆλ‹€.
  • λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  λ•ŒλŠ” self 인자λ₯Ό μ „λ‹¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. self의 값은 인터프리터가 μ œκ³΅ν•©λ‹ˆλ‹€.
  • μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ₯Ό μ •μ˜ν•  λ•Œμ—λŠ” 접두사 self.을 λΆ™μ—¬μ€λ‹ˆλ‹€.

μƒμ„±μž

μƒμ„±μž int

  • ν΄λž˜μŠ€μ— μ˜ν•΄ λ§Œλ“  μΈμŠ€ν„΄μŠ€ 객체의 속성값을 μ΄ˆκΈ°ν™”
  • 클래슀λ₯Ό λ§Œλ“€ λ–„λΆ€ν„° 속성을 지정해주고 κ·Έ 값을 전달.
class Car:
    color = 'red'
    category = 'sports car'

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
##### μ•„λž˜λŠ” colorκ³Ό categoryλ₯Ό μ΄ˆκΈ°ν™”ν•˜λ„λ‘ λ³€κ²½ν•œ μ½”λ“œ
class Car2:
    def __init__(self, color, category):
        self.color = color
        self.category = category

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
  • init λ©”μ„œλ“œ μ•ˆμ— 인자λ₯Ό μ „λ‹¬ν•¨μœΌλ‘œμ¨ μΈμŠ€ν„΄μŠ€ 객체의 속성을 μ΄ˆκΈ°ν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • init λ©”μ„œλ“œ μ•ˆμ— μ •μ˜λœ 속성(λ³€μˆ˜) color와 categoryλŠ” 클래슀λ₯Ό μΈμŠ€ν„΄μŠ€ν™”ν•  λ•Œ 값을 μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 이λ₯Ό μΈμŠ€ν„΄μŠ€ 객체의 μ΄ˆκΈ°ν™” (initializing instance) 라고 히고, initν•¨μˆ˜λŠ” μƒμ„±μž(constructor)라고 ν•©λ‹ˆλ‹€.
  • init μ—­μ‹œ def ν‚€μ›Œλ“œλ‘œ μ •μ˜ν•©λ‹ˆλ‹€. 즉, 클래슀 μ•ˆμ˜ λ©”μ„œλ“œμ΄λ―€λ‘œ self 문법 μžŠμ§€ λ§ˆμ„Έμš”!
#μΈμŠ€ν„΄μŠ€ 객체 μ„ μ–Έ
car1 = Car()
car2 = Car2('yellow', 'sedan')
car1.color
'red'
car2.color
'yellow'
car1.category
'sports car'
car2.category
'sedan'
# μ•„λž˜μ²˜λŸΌ ν‚€μ›Œλ“œ 인자 지정이 κ°€λŠ₯
class Car2:
    def __init__(self, color='red', category='sprots car'):
        self.color = color
        self.category = category

μš”μ•½

  • init이라고 μ“°κ³ , "λ˜λ”(Double Under) 이닛"이라고 λ°œμŒν•©λ‹ˆλ‹€.
  • λ‹€λ₯Έ 객체 지ν–₯ μ–Έμ–΄λ₯Ό μ•Œκ³  μžˆλŠ” λ…μžλΌλ©΄ μƒμ„±μžλΌλŠ” 말을 λ“€μœΌλ©΄ 객체 μΈμŠ€ν„΄μŠ€ν™”μ™€ μ΄ˆκΈ°ν™” 2가지 μž‘μ—…μ„ 생각할 수 μžˆμŠ΅λ‹ˆλ‹€.
  • 파이썬의 μƒμ„±μžλŠ” μ΄ˆκΈ°ν™”λ§Œ μˆ˜ν–‰ν•©λ‹ˆλ‹€. 그럼 객체 μΈμŠ€ν„΄μŠ€ν™”λŠ” λˆ„κ°€ ν• κΉŒμš”? κΈ°μ–΅λ‚˜μ‹œλ‚˜μš”? λ„€, λ°”λ‘œ 클래슀 μ‚¬μš© μ‹œ λ³€μˆ˜ 할당을 톡해 μ΄λ£¨μ–΄μ§‘λ‹ˆλ‹€.
  • init처럼 μ•žλ’€μ— 언더바(_)κ°€ 두 κ°œμ”© μžˆλŠ” λ©”μ„œλ“œλ₯Ό 맀직 λ©”μ„œλ“œ 라고 ν•©λ‹ˆλ‹€.

클래슀 λ³€μˆ˜μ™€ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

class Car:
    Manufacture = "India"   
    # Manufacture λΌλŠ” λ³€μˆ˜, Manufacture λΌλŠ” μΈμŠ€ν„΄μŠ€ κ°€ μ•„λ‹˜(뒀에 ν΄λž˜μŠ€κ°€ μ•„λ‹ˆλ‹ˆκΉŒ.) 
    # κ·Έλƒ₯ ν΄λž˜μŠ€μ— μžˆλŠ” λ³€μˆ˜λΌ 함. 클래슀 λ³€μˆ˜ 

    def __init__(self, color, category='sedan'):
        self.color = color
        self.category = category
        # self.color λΌλŠ” λ³€μˆ˜, self.colorμ΄λΌλŠ” μΈμŠ€ν„΄μŠ€  -> μΈμŠ€ν„΄μŠ€ λ³€μˆ˜
car1 = Car('red','sports car')
car2 = Car('white')
print(car1.Manufacture, car1.color, car1.category)
print(car2.Manufacture, car2.color, car2.category)
India red sports car
India white sedan

κ΅¬λΆ„μ΄μœ λŠ” 객체 λ‹¨μœ„λ‘œ λ°”λ€Œλƒ μ•ˆλ°”λ€Œλƒ

  • μ΄ˆκΈ°ν™”κ°€ κ°€λŠ₯ν•˜κ²Œ ν•˜λŠ” 게 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

클래슀 λ³€μˆ˜

  • ν΄λž˜μŠ€μ— λ°”λ‘œ μ„ μ–Έλœ 속성을 클래슀 λ³€μˆ˜λΌκ³  ν•˜λ©° ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ λͺ¨λ“  κ°μ²΄μ—μ„œ 같은 값을 μ‘°νšŒν•  λ•Œ κ°€λŠ₯ν•©λ‹ˆλ‹€.
  • ManufactureλŠ” 클래슀 λ³€μˆ˜μ—μš”.
  • Manufacture 속성은 car1κ³Ό car2κ°€ 곡유

μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

  • init() μ•ˆμ—μ„œ selfλ₯Ό μ‚¬μš©ν•΄ μ„ μ–Έλœ λ³€μˆ˜λ₯Ό μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λΌκ³  ν•©λ‹ˆλ‹€. 객체가 μΈμŠ€ν„΄μŠ€ν™”λ  λ•Œλ§ˆλ‹€ μƒˆλ‘œμš΄ 값이 ν• λ‹Ήλ˜λ©° μ„œλ‘œ λ‹€λ₯Έ 객체 κ°„μ—λŠ” 값을 κ³΅μœ ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
  • color와 categoryλŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ—μš”.
  • color와 category 속성은 car1κ³Ό car2κ°€ κ³΅μœ ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

상속

ν΄λž˜μŠ€μ— λ©”μ„œλ“œ κΈ°λŠ₯ λͺ‡κ°œλ§Œ μΆ”κ°€ν•˜κ³  싢을 λ•Œ.

class Car:
    Manufacture = "India"

    def __init__(self, color='red', category='sedan'):
        self.color = color
        self.category = category

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)

Car 클래슀의 κΈ°λŠ₯은 μœ μ§€ν•œ 채 maker μ†μ„±λ§Œ μΆ”κ°€λœ μƒˆλ‘œμš΄ 클래슀 NewCarλ₯Ό μ„ μ–Έν•˜κ³  μ‹Άλ‹€

μ†Œκ΄„ν˜Έ μ•ˆμ— 상속받을 클래슀 이름

class 클래슀λͺ…(μ›μ‘°ν΄λž˜μŠ€)

class NewCar(Car):
    pass

car = NewCar()
car.drive()
car.accel(10)
I'm driving
speed up 10 driving at 20
class NewCar(Car):
    maker = 'Porsche'

car = NewCar()
car.maker
'Porsche'

μžμ‹ 클래슀, λΆ€λͺ¨ 클래슀

  • 상속받은 클래슀
    • "μžμ‹ 클래슀", "μ„œλΈŒ 클래슀(sub class)", "νŒŒμƒλœ 클래슀(derived class)"
  • κΈ°μ‘΄ 클래슀
    • "λΆ€λͺ¨ 클래슀", "슈퍼 클래슀(super class)", "베이슀 클래슀(base class)"

상속 μ‚¬μš©ν•˜κΈ°

  • 상속은 기쑴에 μžˆλŠ” μ½”λ“œλ₯Ό μž¬μ‚¬μš©(상속) ν•˜κΈ° μœ„ν•œ κΈ°λŠ₯

λ©”μ„œλ“œ μΆ”κ°€ν•˜κΈ°(add)

class NewCar(Car):
    def fly(self):
        print("I'm flying!! This is the new car!!")

λ©”μ„œλ“œ μž¬μ •μ˜ν•˜κΈ°(override)

  • 기쑴의 λ©”μ†Œλ“œλ₯Ό μˆ˜μ •
class NewCar(Car):
    def fly(self):
        print("I'm flying!! This is the new car!!")

    def drive(self):
        print("I'm driving and can fly")

λΆ€λͺ¨ λ©”μ„œλ“œ ν˜ΈμΆœν•˜κΈ°(super())

def (λΆ€λͺ¨ν΄λž˜μŠ€μ˜)λ©”μ„œλ“œμ΄λ¦„():
_ super().λ©”μ„œλ“œμ΄λ¦„()

class NewCar(Car):
    def __init__(self, color, category, maker):
        super().__init__(color, category)
        self.maker = maker

    def fly(self):
        print("I'm flying!! This is the new car!!")

    def accel(self, speed_up, level=1, current_speed=10):
        self.boost[level] = {1 : 0, 2 : 30, 3 : 50}
        self.speed_up = speed_up + self.boost[level]
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)

class NewCar(Car):
_ def init(self, color, category, maker):
____ ____ self.color = color
____ ____ self.category = category
____ ____ self.maker = maker

μ§€κΈˆμ€ λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ colorλ₯Ό μ΄λ¦„μœΌλ‘œ λ°›μ§€λ§Œ colorλ₯Ό RGB κ°’μœΌλ‘œ 받도둝 λ°”κΎΈκ³  μ‹Άλ‹€κ³  ν•©μ‹œλ‹€. μžλ™μ°¨ 클래슀λ₯Ό 상속받은 λͺ¨λ“  ν΄λž˜μŠ€μ—μ„œ colorλŠ” RGB κ°’μœΌλ‘œ λ°›κ²Œ ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. λ§Œμ•½ λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ“œ ν–ˆλ‹€λ©΄ 상속받은 클래슀 ν•˜λ‚˜ν•˜λ‚˜μ˜ μ΄ˆκΈ°ν™” μ½”λ“œλ₯Ό λ³€κ²½ν•΄ μ£Όμ–΄μ•Ό ν•  κ²λ‹ˆλ‹€. μ΄λ ‡κ²Œ super() λ₯Ό μ‚¬μš©ν•΄ λ³€μˆ˜λ₯Ό μ΄ˆκΈ°ν™”ν•˜λ©΄ λΆ€λͺ¨ 클래슀만 λ³€κ²½ν•˜λ©΄ 되겠죠?

class Car:
    Manufacture = "India"

    def __init__(self, color='red', category='sedan'):
        self.color = color 
        self.category = '2020Y '+ category


class NewCar(Car):
    def __init__(self, color, category, maker):
        super().__init__(color, category)
        self.maker = maker

newcar = NewCar('red','sports car', 'Kia')
print(newcar.category)
2020Y sports car

정리

  1. 클래슀 μ„ μ–Έ

  2. 클래슀 μ‚¬μš©

    • "객체의 μΈμŠ€ν„΄μŠ€"
  3. ν΄λž˜μŠ€λŠ” λ™μž‘κ³Ό μƒνƒœλ₯Ό κ°–λŠ”λ‹€.

    • μƒνƒœ(State): 속성(Attribute)둜 ν‘œν˜„, 일λͺ… λ³€μˆ˜

    • λ™μž‘(Behavior): Methode둜 ν‘œν˜„, 일λͺ… ν•¨μˆ˜

    • κ°μ²΄λŠ” λ™μž‘μ€ κ³΅μœ ν•˜μ§€λ§Œ μƒνƒœλŠ” κ³΅μœ ν•˜μ§€ μ•ŠλŠ”λ‹€.

  4. μƒμ„±μž __ init __

  5. 클래슀 λ³€μˆ˜μ™€ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

    • ν΄λž˜μŠ€μ— μ„ μ–Έλœ 속성은 클래슀 λ³€μˆ˜λΌκ³  ν•˜λ©° 이 ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ λͺ¨λ“  객체에 λŒ€ν•΄ 같은 속성(κ°’)을 κ°–λŠ”λ‹€.
    • 객체가 μΈμŠ€ν„΄μŠ€ν™”λ  λ•Œλ§ˆλ‹€ μƒˆλ‘œμš΄ 값이 ν• λ‹Ήλ˜λ©° μ„œλ‘œ λ‹€λ₯Έ 객체 κ°„μ—λŠ” 속성(κ°’)을 κ³΅μœ ν•  수 μ—†λ‹€.
  6. 상속

    • λ©”μ„œλ“œ μΆ”κ°€, λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ“œ, λΆ€λͺ¨ λ©”μ„œλ“œ ν˜ΈμΆœν•˜κΈ°

n면체 μ£Όμ‚¬μœ„ λ§Œλ“€κΈ°

μ€€λΉ„ ν…œν”Œλ¦Ώ

  1. 기획

n면체 μ£Όμ‚¬μœ„

  1. κΈ°λŠ₯

  • μ£Όμ‚¬μœ„ 면의 개수 n μž…λ ₯
  • λ˜μ§€λ©΄ 1~n 눈의 수 μ€‘μ—μ„œ 랜덀으둜 갖을 좜λ ₯
  1. 정리

  • 객체

    • μ£Όμ‚¬μœ„ : class FunnyDice():
  • μƒνƒœ

    • μ£Όμ‚¬μœ„ λ©΄ : n
    • μ£Όμ‚¬μœ„ 면에 ν• λ‹Ή 숫자 : np
  • λ™μž‘

    • λ˜μ§€κΈ° : throw
    • 랜덀 선택 : randval -> setval
    • κ°’ 좜λ ₯ : prtval -> getval

    main λ§Œλ“€κΈ°

    μž…λ ₯λ°›κΈ° n = get_input()

  • n μ΄λΌλŠ” λ³€μˆ˜λͺ…에 μ£Όμ‚¬μœ„ 면의 수λ₯Ό μž…λ ₯

  • 이 λ³€μˆ˜λŠ” get_input() ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ„œ λ°›κΈ°

    μ£Όμ‚¬μœ„ λ§Œλ“€κΈ° mydice = FunnyDice(n)

  • μ£Όμ‚¬μœ„λŠ” FunnyDice 클래슀의 μΈμŠ€ν„΄μŠ€ mydice

  • mydice λ₯Ό FunnyDice의 ν΄λž˜μŠ€λ‘œλΆ€ν„° μΈμŠ€ν„΄μŠ€ 객체둜(λ„Œ λ‹ˆ μ£Όμ‚¬μœ„ 써)

  • mydice 의 κ°μ²΄λŠ” λ³€μˆ˜ nλ₯Ό 인자둜 λ°›μ•„ 객체의 μ†μ„±μœΌλ‘œ μΆ”κΈ°ν™”μ‹œν‚¬ 수 μžˆλ„λ‘

def main():
    n = get_inputs()
    mydice = FunnyDice(n)
    mydice.throw()
    print("ν–‰μš΄μ˜ μˆ«μžλŠ”? {}".format(mydice.getval()))

FunnyDice 클래슀

FunnyDice 클래슀 λ§Œλ“€κΈ°

class FunnyDice:
    def __init__(self, n):
            self.n = n
    
    def throw(self):
        pass
    
    def getval(self):
        pass

    def setval(self, val):
        pass

랜덀으둜 숫자λ₯Ό μƒμ„±ν•΄μ£ΌλŠ” λͺ¨λ“ˆ import

from random import randrange

μƒμ„±μž __ int __ κ΅¬ν˜„

from random import randrange

class FunnyDice:
    def __init__(self, n=6):                  # n의 λ””ν΄νŠΈ κ°’ 6 
        self.n = int(n)                       # 였λ₯˜λ°©μ§€λ₯Ό μœ„ν•΄ μ •μˆ˜λ‘œ ν˜•λ³€ν™˜
        self.numbers = list(range(1, n+1))    # μ£Όμ‚¬μœ„ λˆˆμˆ«μžλ“€ : 숫자λ₯Ό 리슀트둜 1 ~ n+1
        self.index = randrange(0, self.n)     # number 리슀트의 인덱슀. 
                                              #    μˆ«μžκ°€ 랜덀으둜 λ‚˜μ˜¬ 수 μžˆλ„λ‘ 인덱슀 λ³€μˆ˜ μΆ”κ°€
                                              #    파이썬의 random λͺ¨λ“ˆμ˜ randrange()ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄μ„œ 랜덀으둜 λ‚˜μ˜€κ²Œ
        self.val = self.numbers[self.index]   # μ£Όμ‚¬μœ„μ˜ 눈
  1. νŒŒλΌλ―Έν„° n

    • n의 λ””ν΄νŠΈ κ°’μœΌλ‘œ 6을 지정.
    • μ΄λ ‡κ²Œ ν•˜λ©΄ μΈμŠ€ν„΄μŠ€ 객체λ₯Ό λ§Œλ“€ λ•Œ 인자λ₯Ό 주지 μ•ŠμœΌλ©΄ 6면체 μ£Όμ‚¬μœ„κ°€ λ§Œλ“€μ–΄μ§
    • 그리고 ν˜Ήμ‹œ λͺ°λΌ n을 μ •μˆ˜λ‘œ ν˜•λ³€ν™˜
  2. μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

    • μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ— numbers, index, val을 μΆ”κ°€
    • self.numbersλŠ” μ£Όμ‚¬μœ„ λˆˆλ“€. range() ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄μ„œ 1λΆ€ν„° n+1κΉŒμ§€μ˜ 리슀트λ₯Ό λ§Œλ“¦.
    • self.index λŠ” number 리슀트의 μΈλ±μŠ€μ—μš”. μ£Όμ‚¬μœ„λ₯Ό λ˜μ§€κΈ° μ „ 눈의 수λ₯Ό μœ„ν•΄ 인덱슀 λ³€μˆ˜λ„ μΆ”κ°€ν–ˆμ–΄μš”.
    • μΈλ±μŠ€λŠ” 파이썬 random λͺ¨λ“ˆμ˜ randrange()ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄μ„œ 랜덀으둜 λ‚˜μ˜€κ²Œ ν–ˆμŠ΅λ‹ˆλ‹€.
    • self.val은 μ£Όμ‚¬μœ„μ˜ λˆˆμ΄μ—μš”.

throw 와 getval λ©”μ†Œλ“œ

from random import randrange

class FunnyDice:
    def throw(self):
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]
    
    def getval(self):
        return self.val
  1. throw

    • 랜덀으둜 μ£Όμ‚¬μœ„ 눈이 λ‚˜μ˜€κ²Œ ν•©λ‹ˆλ‹€.
  2. getval

    • μ£Όμ‚¬μœ„ 눈 λ³€μˆ˜ val을 λ°˜ν™˜ν•©λ‹ˆλ‹€. getval λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ μ£Όμ‚¬μœ„ 눈의 값을 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.
    • (μ£Όμ‚¬μœ„ 눈 속성에 λ°”λ‘œ μ ‘κ·Όν•  μˆ˜λ„ μžˆμ§€λ§Œ 일반적으둜 μ΄λ ‡κ²Œ 속성에 μ ‘κ·Όν•˜λŠ” λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜μ—¬ μ œκ³΅ν•©λ‹ˆλ‹€. 파이썬 클린 μ½”λ“œ, Design Pattern κ΄€λ ¨λœ λ‚΄μš©μ΄λ‹ˆ μ°Έκ³ ν•˜μ„Έμš”)

setval λ©”μ†Œλ“œ

from random import randrange

class FunnyDice:    
    def setval(self, val:int):
        if val <= self.n:
            self.val = val
        else:
            msg = "μ£Όμ‚¬μœ„μ— μ—†λŠ” μˆ«μžμž…λ‹ˆλ‹€. μ£Όμ‚¬μœ„λŠ” 1 ~ {0}κΉŒμ§€ μžˆμŠ΅λ‹ˆλ‹€. ".format(self.n)
            raise ValueError(msg)

μ‚¬μš©μžκ°€ μ£Όμ‚¬μœ„ λˆˆμ„ μ„ΈνŒ…ν•  수 μžˆκ²Œλ” ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.
λ§Œμ•½ λ²”μœ„ λ°–μ˜ 숫자λ₯Ό μ„ΈνŒ…ν•˜λ €κ³  ν•˜λ©΄
μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚€λŠ” λ‘œμ§μ„ 쑰건문을 μ΄μš©ν•΄ κ΅¬ν˜„

전체 μ½”λ“œ

class FunnyDice:
    def __init__(self, n=6):
        self.n = int(n)
        self.numbers = list(range(1, n+1))
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]

    def throw(self):
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]

    def getval(self):
        return self.val

    def setval(self, val):
        if val <= self.n:
            self.val = val
        else:
            msg = "μ£Όμ‚¬μœ„μ— μ—†λŠ” μˆ«μžμž…λ‹ˆλ‹€. μ£Όμ‚¬μœ„λŠ” 1 ~ {0}κΉŒμ§€ μžˆμŠ΅λ‹ˆλ‹€. ".format(self.n)
            raise ValueError(msg)
throw = FunnyDice()
lucknum = FunnyDice()
lucknum.throw()
print(lucknum.getval())
lucknum.setval(7)
5



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/2180569561.py in <module>
      3 lucknum.throw()
      4 print(lucknum.getval())
----> 5 lucknum.setval(7)


/var/folders/nv/7gm32z8n6cz1dzxgymhz5lmh0000gn/T/ipykernel_13457/1290511724.py in setval(self, val)
     18         else:
     19             msg = "μ£Όμ‚¬μœ„μ— μ—†λŠ” μˆ«μžμž…λ‹ˆλ‹€. μ£Όμ‚¬μœ„λŠ” 1 ~ {0}κΉŒμ§€ μžˆμŠ΅λ‹ˆλ‹€. ".format(self.n)
---> 20             raise ValueError(msg)


ValueError: μ£Όμ‚¬μœ„μ— μ—†λŠ” μˆ«μžμž…λ‹ˆλ‹€. μ£Όμ‚¬μœ„λŠ” 1 ~ 6κΉŒμ§€ μžˆμŠ΅λ‹ˆλ‹€. 

get_input() ν•¨μˆ˜ λ§Œλ“€μ–΄ 보기

main() ν•¨μˆ˜λ₯Ό 섀계할 λ•Œ, get_inputs()μ—μ„œ μ‚¬μš©μžλ‘œλΆ€ν„° n면체 숫자 n값을 λ°›κ²Œ

def get_inputs():
    n = int(input("μ£Όμ‚¬μœ„ 면의 개수λ₯Ό μž…λ ₯ν•˜μ„Έμš”: "))
    return n

μ΅œμ’… κ²°κ³Ό

# funnydice.py

from random import randrange

class FunnyDice:
    def __init__(self, n=6):
        self.n = n
        self.options = list(range(1, n+1))
        self.index = randrange(0, self.n)
        self.val = self.options[self.index]
    
    def throw(self):
        self.index = randrange(0, self.n)
        self.val = self.options[self.index]
    
    def getval(self):
        return self.val
    
    def setval(self, val):
        if val <= self.n:
            self.val = val
        else:
            msg = "μ£Όμ‚¬μœ„μ— μ—†λŠ” μˆ«μžμž…λ‹ˆλ‹€. μ£Όμ‚¬μœ„λŠ” 1 ~ {0}κΉŒμ§€ μžˆμŠ΅λ‹ˆλ‹€. ".format(self.n)
            raise ValueError(msg)

def get_inputs():
    n = int(input("μ£Όμ‚¬μœ„ 면의 개수λ₯Ό μž…λ ₯ν•˜μ„Έμš”: "))
    return n

def main():
    n = get_inputs()
    mydice = FunnyDice(n)
    mydice.throw()
    print("ν–‰μš΄μ˜ μˆ«μžλŠ”? {0}".format(mydice.getval()))

if __name__ == '__main__':
    main()
μ£Όμ‚¬μœ„ 면의 개수λ₯Ό μž…λ ₯ν•˜μ„Έμš”: 83
ν–‰μš΄μ˜ μˆ«μžλŠ”? 65
profile
λ§ˆμΌ€νŒ…μ„ μœ„ν•œ 인곡지λŠ₯ 섀계와 μŠ€νƒ€νŠΈμ—… Log

0개의 λŒ“κΈ€