----
layout: single
title: Fundamuntal 13. λλ§μ 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'μ΄λΌλ κ°μ΄ κ°λ€
var = 4
var
4
print(id(var), id(4)) #id κ°μ pcλ§λ€ λ€λ¦
λλ€.
4482629968 4482629968
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]
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
\μμ(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)
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'
μλ μμ±κ°μ μ κ·Όνλ©΄ μλ¬κ° λ°μ
# 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)λ‘ λμ
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
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
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)
#μΈμ€ν΄μ€ κ°μ²΄ μ μΈ
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
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
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)
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'
class NewCar(Car):
def fly(self):
print("I'm flying!! This is the new car!!")
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")
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
ν΄λμ€ μ μΈ
ν΄λμ€ μ¬μ©
ν΄λμ€λ λμκ³Ό μνλ₯Ό κ°λλ€.
μν(State): μμ±(Attribute)λ‘ νν, μΌλͺ λ³μ
λμ(Behavior): Methodeλ‘ νν, μΌλͺ ν¨μ
κ°μ²΄λ λμμ 곡μ νμ§λ§ μνλ 곡μ νμ§ μλλ€.
μμ±μ __ init __
ν΄λμ€ λ³μμ μΈμ€ν΄μ€ λ³μ
μμ
n면체 μ£Όμ¬μ
κ°μ²΄
μν
λμ
n μ΄λΌλ λ³μλͺ μ μ£Όμ¬μ λ©΄μ μλ₯Ό μ λ ₯
μ΄ λ³μλ get_input() ν¨μλ₯Ό νΈμΆν΄μ λ°κΈ°
μ£Όμ¬μλ FunnyDice ν΄λμ€μ μΈμ€ν΄μ€ mydice
mydice λ₯Ό FunnyDiceμ ν΄λμ€λ‘λΆν° μΈμ€ν΄μ€ κ°μ²΄λ‘(λ λ μ£Όμ¬μ μ¨)
mydice μ κ°μ²΄λ λ³μ nλ₯Ό μΈμλ‘ λ°μ κ°μ²΄μ μμ±μΌλ‘ μΆκΈ°νμν¬ μ μλλ‘
def main():
n = get_inputs()
mydice = FunnyDice(n)
mydice.throw()
print("νμ΄μ μ«μλ? {}".format(mydice.getval()))
class FunnyDice:
def __init__(self, n):
self.n = n
def throw(self):
pass
def getval(self):
pass
def setval(self, val):
pass
from random import randrange
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] # μ£Όμ¬μμ λ
νλΌλ―Έν° n
μΈμ€ν΄μ€ λ³μ
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
throw
getval
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κΉμ§ μμ΅λλ€.
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