22. 09. 13.

hyo_dยท2022๋…„ 9์›” 13์ผ
0

์บ ํ”„ 10์ผ์ฐจ

1. ์ผ๊ณผ

๐Ÿ‘‰ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•(์ด์ฐฝํ˜ธ ํŠœํ„ฐ๋‹˜)

๐Ÿ‘‰ ํŒŒ์ด์ฌ ๊ณผ์ œ(ํด๋ž˜์Šค ํ™œ์šฉํ•˜๊ธฐ)

2. Class

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'
}
"""

3. mutable ์ž๋ฃŒํ˜•๊ณผ immutable ์ž๋ฃŒํ˜•

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๊ฐœ๋ฅผ ๊นƒํ—™์— ์˜ฌ๋ ค๋ณด์•˜๋‹ค. ์ œ๋Œ€๋กœ ๋œ ๊ฑด์ง€ ์ž˜ ์•Œ ์ˆ˜ ์—†์ง€๋งŒ ์šฐ์„  ๊นƒํ—™์—์„œ ํ™•์ธํ–ˆ์„๋•Œ ๋ฌธ์ œ ์—†์ด ์ปค๋ฐ‹๋˜์—ˆ๋‹ค. ๊นƒ, ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•, ๊ณผ์ œ ๋“ฑ ์ฐจ๊ทผ์ฐจ๊ทผ ํ•˜๋‚˜์”ฉ ํ•ด๋‚˜๊ฐ€๋ฉด์„œ ๋‚ด์ผ๋ถ€ํ„ฐ ์žฅ๊ณ  ์˜ˆ์Šต๋„ ์‹œ์ž‘ํ•ด์•ผ๊ฒ ๋‹ค.

profile
ํ–‡๋ณ‘์•„๋ฆฌ

0๊ฐœ์˜ ๋Œ“๊ธ€