Types
λ³μλ€μ νμ
μ type() ν¨μλ₯Ό μ΄μ©νμ¬ κ΅¬ν μ μλ€.
Class
ν΄λμ€λ λ°μ΄ν° νμ
μ ν
νλ¦Ώμ΄λ€. class ν€μλλ₯Ό ν΅ν΄ ν΄λμ€λ₯Ό μ μνλ©°, ν΄λμ€μ μ΄λ¦ μμ λλ¬Έμλ₯Ό μ΄λ€. λ§μ½ μλμ μΌλ‘ class λ΄λΆλ₯Ό λΉμ°κ³ μΆλ€λ©΄ pass ν€μλλ₯Ό μ¬μ©νλ€.
ꡬ문
class [ν΄λμ€ λͺ
]:
Instantiation
ν΄λμ€λ λ°λμ μΈμ€ν΄μ€ν λμ΄μΌ νλ€. μ¦ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λ§λ€μ΄μΌ νλ€λ λ»μ΄λ€. μΈμ€ν΄μ€μ νλκ²μ ν¨μλ₯Ό νΈμΆνλ κ²κ³Ό λΉμ·νλ€.
[λ³μλͺ
] = [ν΄λμ€λͺ
]()
Object-Oriented Programming
ν΄λμ€μ μΈμ€ν΄μ€λ κ°μ²΄λΌκ³ λ λΆλ¦°λ€. ν΄λμ€λ₯Ό μ μνκ³ κ°μ²΄λ₯Ό λ§λλ ν¨ν΄μ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ° μ΄λΌκ³ λΆλ₯Έλ€.
ν΄λμ€μ type() λ©μλλ₯Ό μ¬μ©νλ©΄ <class '__main__.[ν΄λμ€λͺ
]'> κ³Ό κ°μ κ²°κ³Όκ° λμ¨λ€.
__main__ μ βνμ¬ μ΄ νμΌμ΄ μ€νλκ³ μλ€β λΌλ μλ―Έμ΄λ€.
Class Variables
ν΄λμ€μ λͺ¨λ μΈμ€ν΄μ€μμ λμΌν λ°μ΄ν°λ₯Ό μ¬μ©νλ €λ©΄ ν΄λμ€ λ³μλ₯Ό μ¬μ©νλ©΄ λλ€. ν΄λμ€ λ³μλ ν΄λμ€μ λͺ¨λ μΈμ€ν΄μ€μ λν΄ λμΌν λ³μμ΄λ€. ν΄λμ€ λ΄λΆμ λ€μ¬μ°κΈ° ν λ³μλ₯Ό μ μν μ μμΌλ©°, [object λͺ
].[variable λͺ
] ꡬ문μ μ¬μ©νμ¬ ν΄λμ€ λ΄λΆμ λͺ¨λ λ³μμ μ κ·Ό ν μ μλ€.
[μμ μ½λ]
class Musician:
title = "Rockstar"
drummer = Musician()
print(drummer.title)
# prints "Rockstar"
Python
볡μ¬
Methods
ν΄λμ€ μμ λ©μλκ° ν¬ν¨ λ μλ μλ€. λ©μλ μμ ν¨μ λν μ¬μ©ν μ μλλ°, λ€μ μμλ₯Ό 보면 μ½κ² μ΄ν΄κ° κ²μ΄λ€.
[μμ μ½λ]
class Dog:
dog_time_dilation = 7
def time_explanation(self):
print("Dogs experience {} years for every 1 human year.".format(self.dog_time_dilation))
pipi_pitbull = Dog()
pipi_pitbull.time_explanation()
# Prints "Dogs experience 7 years for every 1 human year."
Python
볡μ¬
ν΄λμ€κ° μ¬λ¬ argument λ₯Ό μ¬μ©νλ λ©μλλ₯Ό κ°μ§ μλ μλ€. μλμ μμλ₯Ό νμΈν΄λ³΄μ.
class Circle:
pi = 3.14
def area(self, radius):
return Circle.pi * radius ** 2
circle = Circle()
pizza_area = circle.area(12/2)
teaching_table_area = circle.area(36/2)
round_room_area = circle.area(11460/2)
Python
볡μ¬
Constructors
__init__() λ©μλλ μλ‘κ² μμ±λ κ°μ²΄λ₯Ό μ΄κΈ°ν νκΈ° μν΄ μ¬μ©νλ€. Python μμλ μ΄λ₯Ό constructor, μμ±μλΌκ³ λ νλ€. μλ μμλ₯Ό ν΅ν΄ λ©μλκ° μ΄λ»κ² μ°μ΄λμ§ νμΈν΄λ³΄μ.
class Shouter:
def __init__(self, phrase):
# make sure phrase is a string
if type(phrase) == str:
# then shout it out
print(phrase.upper())
shout1 = Shouter("shout")
# prints "SHOUT"
shout2 = Shouter("shout")
# prints "SHOUT"
shout3 = Shouter("let it all out")
# prints "LET IT ALL OUT"
Python
볡μ¬
Instance Variables
κ°μ²΄κ° 보μ ν λ°μ΄ν°λ₯Ό μΈμ€ν΄μ€ λ³μλΌκ³ νλ€. μΈμ€ν΄μ€ λ³μλ ν΄λμ€μ λͺ¨λ μΈμ€ν΄μ€μμ 곡μ λμ§ μκ³ , μ°κ²°λ κ°μ²΄μ νΉμ ν λ³μκ° λλ€.
μ΄λ ν΄λμ€ κ°κ°μ μΈμ€ν΄μ€κ° λ€λ₯Έ μ’
λ₯μ λ°μ΄ν°λ₯Ό κ°μ§ λ μ¬μ©νλ€.
Attribute Functions
μΈμ€ν΄μ€ λ³μμ ν΄λμ€ λ³μλ λͺ¨λ λ λ€ κ°μ²΄μ μμ±μΌλ‘ κ°μ£Όλλ€. ν΄λμ€ λ³μλ μλκ³ κ°μ²΄μ μΈμ€ν΄μ€ λ³μλ μλ μμ±μ μ‘μΈμ€νλ €κ³ νλ©΄ AttributeError κ° λνλλ€. λ§μ½ κ°μ²΄μ μμ±μ΄ μλμ§ μ¬λΆλ₯Ό νμ ν μ μλ€λ©΄ μ΄λ»κ² ν΄μΌ ν κΉ? hasattr() μ κ°μ²΄μ μ§μ λ μμ±μ΄ μμΌλ©΄ Trueλ₯Ό λ°ννκ³ κ·Έλ μ§ μμΌλ©΄ Falseλ₯Ό λ°ννλ€. λ§μ½ μ°λ¦¬κ° μμ±μ μ€μ κ°μ μ»κ³ μΆλ€λ©΄, getattr() μ μ¬μ©νλ€. μ΄λ μ£Όμ΄μ§ κ°μ²΄μ μμ±μ κ°μ λ°ννλ€. λ§μ½ κ°μ²΄μ μ£Όμ΄μ§ μμ±μ΄ μμ κ²½μ° κΈ°λ³Έκ°μ΄ λλ μΈ λ²μ§Έ νλΌλ―Έν°λ₯Ό λ°νν μλ μλ€.
hasattr(object, "attribute")
β’
object : νΉμ μμ±μ΄ μλμ§ νμΈνκΈ° μν΄ ν
μ€νΈ μ€μΈ κ°μ²΄
β’
attribute : μ‘΄μ¬νλμ§ νμΈν μμ± μ΄λ¦
getattr(object, "attribute", default)
β’
object : νΉμ±μ νκ°ν κ°μ²΄
β’
attribute : μ°λ¦¬κ° νκ°ν μμ± μ΄λ¦
β’
default : μμ±μ΄ μλ κ²½μ° λ°νλλ κ° (μ°Έκ³ : μ΄ λ§€κ° λ³μλ μ ν μ¬νμ
λλ€)
μ΄λ¬ν ν¨μλ₯Ό νΈμΆνλ λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
Self
μμ μ½λλ‘ μ΄ν΄νμ.
[μμ μ½λ]
class SearchEngineEntry:
secure_prefix = "https://"
def __init__(self, url):
self.url = url
def secure(self):
return "{prefix}{site}".format(prefix=self.secure_prefix, site=self.url)
codecademy = SearchEngineEntry("www.codecademy.com")
wikipedia = SearchEngineEntry("www.wikipedia.org")
print(codecademy.secure())
# prints "https://www.codecademy.com"
print(wikipedia.secure())
# prints "https://www.wikipedia.org"
Python
볡μ¬
class Circle:
pi = 3.14
def __init__(self, diameter):
print("Creating circle with diameter {d}".format(d=diameter))
# Add assignment for self.radius here:
self.radius = diameter / 2
def circumference(self):
return 2 * self.pi * self.radius
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
print(medium_pizza.circumference())
print(teaching_table.circumference())
print(round_room.circumference())
Python
볡μ¬
Everything is an Object
dir() ν¨μλ₯Ό μ¬μ©νμ¬ λ°νμ μ€μ μλ κ°μ²΄μ μμ±μ μ‘°μ¬ν μ μλ€. dir() λ λλ ν°λ¦¬μ μ€μλ§μ΄λ©° κ°μ²΄ μμ±μ 체κ³μ μΌλ‘ νμνλ€.
String Representation
__repr()__ μ΄κ²μ μ°λ¦¬κ° μνλ ν΄λμ€μ λ¬Έμμ΄ ννμ Pythonμ μ리λ λ° μ¬μ©ν μ μλ λ°©λ²μ΄λ€. __repr()__ μ νλμ λ§€κ° λ³μμΈ self λ§ κ°μ§ μ μμΌλ©° λ¬Έμμ΄μ λ°νν΄μΌ νλ€. μλμ μμλ₯Ό ν΅ν΄ μ΄ν΄νλλ‘ νμ.
[μμ μ½λ]
class Employee():
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
argus = Employee("Argus Filch")
print(argus)
# prints "Argus Filch"
Python
볡μ¬
class Circle:
pi = 3.14
def __init__(self, diameter):
self.radius = diameter / 2
def area(self):
return self.pi * self.radius ** 2
def circumference(self):
return self.pi * 2 * self.radius
def __repr__(self):
return "Circle with radius {radius}".format(radius=self.radius)
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
print(medium_pizza)
print(teaching_table)
print(round_room)
Python
볡μ¬