๐ ํ์ด์ฌ ๋ฌธ๋ฒ(์ด์ฐฝํธ ํํฐ๋)
๐ ํ์ด์ฌ ๊ณผ์ (ํด๋์ค ํ์ฉํ๊ธฐ)
1) Class๋?
ํด๋์ค๋ฅผ ์ ์ธํ๋๊ฒ์ ๊ณผ์ ํ์ ๋ง๋๋ ๊ฒ์ด๊ณ , ์ ์ธ๋ ๊ณผ์ํ(class)๋ก
๊ณผ์(instance)๋ฅผ ๋ง๋ ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
์ ์ธ ํ ๋ฐ๋ก ์ฌ์ฉ๋๋ ํจ์์ ๋ค๋ฅด๊ฒ ํด๋์ค๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ์ฌ์ฉํ๊ฒ ๋๋ค.
class ๋ด๋ถ์ ์ ์ธ๋๋ ๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก self๋ผ๋ ์ธ์๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
self๋ ํด๋์ค ๋ด์์ ์ ์ญ ๋ณ์์ ๊ฐ์ด ์ฌ์ฉ๋๋ค.
# ์ฉ์ด ์ ๋ฆฌ
- ์ธ์คํด์ค(instance) : class๋ฅผ ์ฌ์ฉํด ์์ฑ๋ ๊ฐ์ฒด
- ๋ฉ์๋(method) : ๋ฉ์๋๋ ํด๋์ค ๋ด์ ์ ์ธ๋ ํจ์์ด๋ฉฐ, ํด๋์ค ํจ์๋ผ๊ณ ๋ ํ๋ค.
- self : ๋ฉ์๋๋ฅผ ์ ์ธํ ๋์๋ ํญ์ ์ฒซ๋ฒ์งธ ์ธ์๋ก self๋ฅผ ๋ฃ์ด์ค์ผ ํ๋ค.
2) Class์ ๊ธฐ๋ณธ ๊ตฌ์กฐ
class CookieFrame(): # CookieFrame์ด๋ผ๋ ์ด๋ฆ์ class ์ ์ธ
def set_cookie_name(self, name):
self.name = name
cookie1 = CookieFrame()
cookie2 = CookieFrame()
cookie1.set_cookie_name("cookie1") # ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์ self๋ ๋ฌด์๋๋ค.
cookie2.set_cookie_name("cookie2")
print(cookie1.name) # cookie1
print(cookie2.name) # cookie2
3) init ํจ์
# class์ __init__๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ธ์คํด์ค ์์ฑ ์ ํด๋น ๋ฉ์๋๊ฐ ์คํ๋๋ค.
class CookieFrame():
def __init__(self, name):
print(f"์์ฑ ๋ ๊ณผ์์ ์ด๋ฆ์ {name} ์
๋๋ค!")
self.name = name
cookie1 = CookieFrame("cookie1") # ์์ฑ ๋ ๊ณผ์์ ์ด๋ฆ์ cookie1 ์
๋๋ค!
cookie2 = CookieFrame("cookie2") # ์์ฑ ๋ ๊ณผ์์ ์ด๋ฆ์ cookie2 ์
๋๋ค!
4) Class ํ์ฉํ๊ธฐ
from pprint import pprint
class Profile:
def __init__(self):
self.profile = {
"name": "-",
"gender": "-",
"birthday": "-",
"age": "-",
"phone": "-",
"email": "-",
}
def set_profile(self, profile):
self.profile = profile
def get_profile(self):
return self.profile
profile1 = Profile()
profile2 = Profile()
profile1.set_profile({
"name": "lee",
"gender": "man",
"birthday": "01/01",
"age": 32,
"phone": "01012341234",
"email": "python@sparta.com",
})
profile2.set_profile({
"name": "park",
"gender": "woman",
"birthday": "12/31",
"age": 26,
"phone": "01043214321",
"email": "flask@sparta.com",
})
pprint(profile1.get_profile())
pprint(profile2.get_profile())
# result print
"""
{
'name': 'lee',
'gender': 'man',
'birthday': '01/01',
'age': 32,
'phone': '01012341234',
'email': 'python@sparta.com'
}
{
'name': 'park',
'gender': 'woman',
'birthday': '12/31',
'age': 26,
'phone': '01043214321',
'email': 'flask@sparta.com'
}
"""
1) ๋น๊ต
muteble์ ๊ฐ์ด ๋ณํ๋ค๋ ์๋ฏธ์ด๋ฉฐ, immutable์ ๊ฐ์ด ๋ณํ์ง ์๋๋ค๋ ์๋ฏธ๋ค.
int, str, list ๋ฑ ์๋ฃํ์ ๊ฐ๊ฐ muteble ํน์ immutebleํ ์์ฑ์ ๊ฐ์ง๊ณ ์๋ค.
a = 10
b = a
b += 5
์ ๊ฐ์ ์ฝ๋๊ฐ ์์ ๋ a์ ๋ด๊ธด ์๋ฃํ์ด muteble ์์ฑ์ธ์ง immuteble ์์ฑ์ธ์ง์ ๋ฐ๋ผ
์ถ๋ ฅํ์ ๋ ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง๊ฒ ๋๋ค.
immutable ์์ฑ์ ๊ฐ์ง ์๋ฃํ : int, float, str, tuple
mutable ์์ฑ์ ๊ฐ์ง ์๋ฃํ : list, dict
2) ์ฝ๋์์์ ์ฐจ์ด ๋น๊ต
immutable = "String is immutable!!"
mutable = ["list is mutable!!"]
string = immutable
list_ = mutable
string += " immutable string!!"
list_.append("mutable list!!")
print(immutable)
print(mutable)
print(string)
print(list_)
# result print
"""
String is immutable!!
['list is mutable!!', 'mutable list!!'] -> ๋ฆฌ์คํธ์ ํน์ฑ์ ๋ณ๋์ด ์๊ธด๋ค!!
String is immutable!! immutable string!!
['list is mutable!!', 'mutable list!!']
"""
โญ 4์ผ๊ฐ์ ์ถ์ ์ฐํด๊ฐ ๋๋๊ณ ๋ค์ ๊ณต๋ถ๋ฅผ ์์ํ๋ ๋๋ฌด ํ ๊ฒ ๋ง๋ค.. ์๊ณ ๋ฆฌ์ฆ๋ ํ์ด์ผํ๊ณ ๋งค์ผ ์ถ์ ๋๋ ๊ณผ์ ๋ค๋ ํด์ผํ๊ณ ๋ณต์ต๋ ํด์ผํ๊ณ ๐ฅ ์ค๋์ ์ฒ์์ผ๋ก ๊ณผ์ 3๊ฐ๋ฅผ ๊นํ์ ์ฌ๋ ค๋ณด์๋ค. ์ ๋๋ก ๋ ๊ฑด์ง ์ ์ ์ ์์ง๋ง ์ฐ์ ๊นํ์์ ํ์ธํ์๋ ๋ฌธ์ ์์ด ์ปค๋ฐ๋์๋ค. ๊น, ํ์ด์ฌ ๋ฌธ๋ฒ, ๊ณผ์ ๋ฑ ์ฐจ๊ทผ์ฐจ๊ทผ ํ๋์ฉ ํด๋๊ฐ๋ฉด์ ๋ด์ผ๋ถํฐ ์ฅ๊ณ ์์ต๋ ์์ํด์ผ๊ฒ ๋ค.