Search
Duplicate

Classes

νƒœκ·Έ
클래슀
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
볡사