[TIL] Python Study - Inheritance
Inheritance
Category: Python
, OOP
class Puppy:
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
def woof_woof(self):
print("Woof Woof!")
def introduce(self):
self.woof_woof()
print(f"My name is {self.name} and I am a baby {self.breed}")
self.woof_woof()
teemo = Puppy(
name="Teemo",
breed="Goldendoodle",
age=1.6
)
teemo.introduce()
- classλ ν¨μλ€μ λ¬Άμ΄μ£Όλ μν μ νλ€.
class ClassName:
ν΄λμ€ μ΄λ¦μ 첫κΈμ λλ¬Έμ νκΈ°
def __init__(self)
μ κ°μ²΄(object)μ μμ±(attributes)μ μ΄κΈ°ννκ³ μ΄κΈ° μνλ₯Ό μ€μ νλ€.def __init__(self, μμ±1, μμ±2, μμ±3): self.μμ±1 = κ° self.μμ±2 = κ° self.μμ±3 = κ°
objectname = Classname( μμ±1="κ°", μμ±2="κ°', μμ±3="κ° )
κ°μ²΄ μ΄λ¦μ μλ¬Έμ νκΈ°- ν¨μλ₯Ό νΈμΆν λ
objectname.function()
ν΄λμ€μ μ€λ³΅μ΄ λ§μ λλ μ΄λ»κ² μ²λ¦¬ν κΉ?
class GuardDog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
self.age = 5
def rrrrr(self):
print("stay away!")
class Puppy:
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
def woof_woof(self):
print("Woof Woof!")
- GuardDog ν΄λμ€μ Puppy ν΄λμ€λ λ λ€ Dog λΌλ 곡ν΅μ μ΄ μλ€.
- μ΄λ₯Ό λ½μλ΄ λΆλͺ¨ ν΄λμ€λ₯Ό μμ±ν μ μλ€.
class Dog:
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
class GuardDog(Dog):
def rrrrr(self):
print("stay away!")
class Puppy(Dog):
def woof_woof(self):
print("woof woof!")
teemo = Puppy(
name="Teemo",
breed="Goldendoodle",
)
leo = GuardDog(
name="Leo",
breed="Shepard"
)
print(leo)
# μΆλ ₯:
# TypeError: Dog.__init__() missing 1 required
# positional argument: 'age'
- Dog ν΄λμ€λ₯Ό μμ±ν΄ 곡ν΅λλ μμ±μ λ½μ
__init__
λ©μλλ‘ μ΄κΈ°ννκ³ GuardDog ν΄λμ€μ Puppy ν΄λμ€μ init λ©μλλ₯Ό μμ νλ€. - μ΄ λ μ€ννλ©΄ μ½μ μ°½μμ νμ
μλ¬λ₯Ό λ³΄κ² λλλ°,
__init__
λ©μλκ° νΈμΆλ λage
λΌλ 맀κ°λ³μμ λν μμΉ μΈμ(κ°)κ° λλ½λμλ€λ μλ―Έμ΄λ€.
class Dog:
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
def sleep(self):
print("zZzZzZzZzZ.....")
class GuardDog(Dog):
def __init__(self, name, breed):
super().__init__(
name,
breed,
5,
)
def rrrrr(self):
print("stay away!")
class Puppy(Dog):
def __init__(self, name, breed):
super().__init__(
name,
breed,
0.1,
)
def woof_woof(self):
print("woof woof!")
teemo = Puppy(
name="Teemo",
breed="Goldendoodle",
)
leo = GuardDog(
name="Leo",
breed="Shepard"
)
teemo.sleep()
leo.sleep()
super()
νμ΄μ¬ ν¨μ. μμ κ΄κ³μμ ChildClass μμ ParentClass μ λ©μλλ₯Ό νΈμΆν λ μ¬μ©λλ€. ChildClass μμ ParentClass μ λ©μλλ₯Ό μ€λ²λΌμ΄λ(μ¬μ μ)νκ³ , κ·Έ μ€λ²λΌμ΄λλ λ©μλμμ ParentClass μ λ©μλλ₯Ό νΈμΆν λ μ¬μ©λλ€.
PEP 8 (Python Enhancement Proposal 8)
μΌνλ₯Ό μ νμ μΌλ‘ λ€μ λΆμ΄λ μ€νμΌμ PEP 8 (Python Enhancement Proposal 8) μ€νμΌ κ°μ΄λμ λͺ μλμ΄ μλ€. PEP 8μ νμ΄μ¬ μ½λμ μ€νμΌκ³Ό ꡬ쑰μ κ΄ν κΆμ₯ μ¬νμ μ μνλ 곡μ λ¬Έμμ΄λ€. PEP 8μμλ μ¬λ¬ μ€λ‘ λμ΄λ μμλ€μ λ§μ§λ§μ μΌνλ₯Ό μΆκ°νλ κ²μ κΆμ₯νκ³ μλ€.
μ¬λ¬ μ€λ‘ λμ΄λ μμλ€μ λν κ΄λ ¨ λ΄μ©μ PEP 8 λ¬Έμμ βWhen to use trailing commasβ μΉμ μ κΈ°μ λμ΄ μλ€. μ΄ λΆλΆμμλ μ¬λ¬ μ€λ‘ λμ΄λ μμλ€μ λ§μ§λ§μ μΌνλ₯Ό μΆκ°νλ κ²μ΄ κ°λ μ±κ³Ό μ μ§λ³΄μμ±μ ν₯μμν¬ μ μλ€κ³ μ€λͺ νκ³ μλ€.
μλ₯Ό λ€μ΄, λ€μμ PEP 8μ λ°λΌ μ¬λ¬ μ€μ μμλ₯Ό λμ΄ν λ μΌνλ₯Ό μ¬μ©νλ μμμ΄λ€:
pythonCopy code
# PEP 8 μ€νμΌ
colors = [
'red',
'green',
'blue',
]
# μΌνλ₯Ό μ¬μ©νμ§ μμ κ²½μ°
colors = [
'red',
'green',
'blue'
]
μΌνλ₯Ό μ¬μ©νλ©΄ μμλ₯Ό μΆκ°νκ±°λ μ κ±°ν λ μ½λ λ³κ²½μ΄ μ©μ΄νλ©°, μ½λ 리뷰λ λ²μ κ΄λ¦¬ μμλ λ³κ²½ λ΄μ©μ΄ λͺ ννκ² νμλλ€. μ΄λ¬ν μ΄μ λ‘ PEP 8μμλ μΌνλ₯Ό μ¬λ¬ μ€λ‘ λμ΄λ μμλ€μ λ§μ§λ§μ μΆκ°νλ κ²μ κΆμ₯νκ³ μλ€.
Python Code Test Practice
[PCCE κΈ°μΆλ¬Έμ ] 6λ² / κ°μ±μ (λλ²κΉ λ¬Έμ )
def solution(numbers, our_score, score_list):
answer = []
for i in range(len(numbers)):
if our_score[i] == score_list[numbers[i]-1]:
# debug: numbers[our_score[i]] == score_list[i]
answer.append("Same")
else:
answer.append("Different")
return answer
'''
range()λ 0λΆν° μ£Όμ΄μ§ μ«μ μ§μ κΉμ§μ μ μλ₯Ό μμ±νλ ν¨μ:
리μ€νΈμ²λΌ λμνμ§λ§ 리μ€νΈκ° μλλ€.
μ΄λ° κ°μ²΄λ₯Ό 'μ΄ν°λ¬λΈ' μ΄λΌκ³ λΆλ₯Έλ€. for 루νμ κ°μ΄ μ°μΈλ€.
'''
for i in range(3):
print(i)
# μΆλ ₯:
# 1
# 2
# 3
'''μ€ν
(step)μ μ§μ νλ κ²λ κ°λ₯νλ€.'''
for i in range(2, 10, 2):
print(i)
# μΆλ ₯:
# 2
# 4
# 6
# 8
'''len()μ μνμ€μ κΈΈμ΄λ₯Ό λ°ννλ ν¨μ:'''
my_list = [a, b, c]
length_of_list = len(my_list)
print(length_of_list)
# μΆλ ₯:
# 3