๐ก ๋ณธ ํฌ์คํ ์ ์์ ๋ด์ฉ, ๊ต์ฌ, ChatGPT๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ ๋ฆฌํ ํ์ต ๊ธฐ๋ก์ ๋๋ค.
๊ธ๊ณผ ๊ทธ๋ฆผ ๋ฑ ๋ชจ๋ ์ฝํ ์ธ ์ ์ ๋ฆฌ ๋ฐ ์์ฑ์ ๋ณธ ๋ธ๋ก๊ทธ์์ ์ง์ ์ ์๋์์ต๋๋ค.
Python ๊ธฐ์ด๋ถํฐ Streamlit ๋ฐ์ดํฐ ์๊ฐํ๊น์ง ์ ๋ฆฌํ๋ ํ์ต ๊ธฐ๋ก์
๋๋ค.
์ด ๊ธ์ '16. [ํ์ด์ฌ] class method vs static method' ์ ๋๋ค.
Python ํด๋์ค์์๋ ๋ฉ์๋๋ฅผ ์ ์ํ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ธ์คํด์ค ๋ฉ์๋(instance method) ๋ฅผ ์ฌ์ฉํ๋ค.
ํ์ง๋ง ํด๋์ค ๊ตฌ์กฐ๋ฅผ ์ค๊ณํ๋ค ๋ณด๋ฉด ํด๋์ค ์์ฒด์ ๊ด๋ จ๋ ๊ธฐ๋ฅ์ด๋ ๊ฐ์ฒด ์ํ์ ๋ฌด๊ดํ ์ ํธ ๊ธฐ๋ฅ์ด ํ์ํ ๋๋ ์๋ค.
์ด๋ ์ฌ์ฉํ๋ ๊ฒ์ด class method์ static method์ด๋ค.
์ด๋ฒ ๊ธ์์๋ instance method, class method, static method์ ์ฐจ์ด์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด๋ณธ๋ค.
์ธ์คํด์ค ๋ฉ์๋ (Instance Method)
์ธ์คํด์ค ๋ฉ์๋๋ ๊ฐ์ฒด๋ฅผ ํตํด ํธ์ถ๋๋ ๊ธฐ๋ณธ์ ์ธ ๋ฉ์๋์ด๋ค.
์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก self๋ฅผ ์ฌ์ฉํ๋ฉฐ, ํ์ฌ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ ํด๋์ค๋ฅผ ๋ณด์.
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
def show_info(self):
print(self.name, self.score)
๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
s = Student("Matt", 90)
s.show_info()
์ฌ๊ธฐ์ show_info()๋ ๊ฐ์ฒด์ ์ํ๋ฅผ ์ฌ์ฉํ๋ ๋ฉ์๋์ด๋ค.
์ฆ, ์ธ์คํด์ค ๋ฉ์๋๋ ๊ฐ์ฒด ๋ฐ์ดํฐ์ ์ง์ ์ ์ผ๋ก ์ฐ๊ฒฐ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ ๋ ์ฌ์ฉํ๋ค.
ํด๋์ค ๋ฉ์๋ (Class Method)
ํด๋์ค ๋ฉ์๋๋ ํด๋์ค ์์ฒด์ ๊ด๋ จ๋ ๋์์ ์ ์ํ ๋ ์ฌ์ฉํ๋ค.
ํด๋์ค ๋ฉ์๋๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋ @classmethod ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ค.
class Student:
school = "Python School"
def __init__(self, name):
self.name = name
@classmethod
def show_school(cls):
print(cls.school)
์ฌ๊ธฐ์ cls๋ ํด๋์ค ์์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๋งค๊ฐ๋ณ์์ด๋ค.
๋ฉ์๋๋ฅผ ํธ์ถํ ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
Student.show_school()
๋๋ ๊ฐ์ฒด๋ฅผ ํตํด ํธ์ถํ ์๋ ์๋ค.
s = Student("Matt")
s.show_school()
ํด๋์ค ๋ฉ์๋๋ ๋ณดํต ํด๋์ค ์์ฑ(class variable)์ ๋ค๋ฃฐ ๋ ์ฌ์ฉ๋๋ค.
์ ์ ๋ฉ์๋ (Static Method)
์ ์ ๋ฉ์๋๋ ํด๋์ค์ ๊ด๋ จ๋์ด ์์ง๋ง ๊ฐ์ฒด ์ํ๋ ํด๋์ค ์ํ๋ฅผ ์ฌ์ฉํ์ง ์๋ ๋ฉ์๋์ด๋ค.
@staticmethod ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ํ๋ค.
class Calculator:
@staticmethod
def add(a, b):
return a + b
์ ์ ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ์ด ํธ์ถํ ์ ์๋ค.
result = Calculator.add(3, 5)
print(result)
์ ์ ๋ฉ์๋๋ self๋ cls๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค.
์ฆ, ํด๋์ค ๋ด๋ถ์ ์์ง๋ง ๋ ๋ฆฝ์ ์ธ ํจ์์ฒ๋ผ ๋์ํ๋ค.
์ธ ๊ฐ์ง ๋ฉ์๋ ๋น๊ต
์ธ ๊ฐ์ง ๋ฉ์๋๋ฅผ ๋น๊ตํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
| ๊ตฌ๋ถ | ์ฒซ ๋ฒ์งธ ์ธ์ | ์ ๊ทผ ๋์ |
| Instance Method | self | ๊ฐ์ฒด |
| Class Method | cls | ํด๋์ค |
| Static Method | ์์ | ์์ |
์ ๋ฆฌํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ดํดํ ์ ์๋ค.
- Instance Method → ๊ฐ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ฌ์ฉ
- Class Method → ํด๋์ค ์ ๋ณด๋ฅผ ๋ค๋ฃฐ ๋ ์ฌ์ฉ
- Static Method → ํด๋์ค์ ๊ด๋ จ๋ ์ผ๋ฐ ๊ธฐ๋ฅ
๊ฐ๋จํ ์์
์ธ ๊ฐ์ง ๋ฉ์๋๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ ์์ ๋ฅผ ๋ณด์.
class Example:
count = 0
def __init__(self):
Example.count += 1
def instance_method(self):
print("instance method")
@classmethod
def class_method(cls):
print("instance count:", cls.count)
@staticmethod
def static_method():
print("static method")
์ฌ์ฉ ์์
e1 = Example()
e2 = Example()
e1.instance_method()
Example.class_method()
Example.static_method()
์ด์ฒ๋ผ ๊ฐ ๋ฉ์๋๋ ์๋ก ๋ค๋ฅธ ๋ชฉ์ ์ ๊ฐ์ง๊ณ ์ฌ์ฉ๋๋ค.
์ ๋ฆฌ
Python ํด๋์ค์์๋ ์ธ ๊ฐ์ง ์ ํ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- instance method → ๊ฐ์ฒด ์ํ๋ฅผ ์ฌ์ฉํ๋ ๋ฉ์๋
- class method → ํด๋์ค ์์ฒด์ ๊ด๋ จ๋ ๋ฉ์๋
- static method → ํด๋์ค ๋ด๋ถ์ ์์ง๋ง ๋ ๋ฆฝ์ ์ธ ๊ธฐ๋ฅ
ํนํ @classmethod์ @staticmethod๋ ํด๋์ค ๊ตฌ์กฐ๋ฅผ ์ค๊ณํ ๋ ์ ์ฉํ๊ฒ ํ์ฉ๋๋ค.
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์์๋ ์ด๋ฌํ ๋ฉ์๋ ๊ตฌ์กฐ๋ฅผ ์ ์ ํ ํ์ฉํ๋ฉด ์ฝ๋๋ฅผ ๋ ๋ช ํํ๊ณ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ์ค๊ณํ ์ ์๋ค.
๋ค์ ๊ธ์์๋ ํจ์์ ๋์์ ํ์ฅํ๋ ๊ณ ๊ธ ๊ฐ๋ ์ธ ํด๋ก์ ์ ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ดํด๋ณธ๋ค.
'Python & SQL > Python Basics' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| 18. [ํ์ด์ฌ] ๋ชจ๋๊ณผ ํ๋ก์ ํธ ๊ตฌ์กฐ (2) | 2026.03.08 |
|---|---|
| 17. [ํ์ด์ฌ] ํด๋ก์ ์ ๋ฐ์ฝ๋ ์ดํฐ (2) | 2026.03.08 |
| 15. [ํ์ด์ฌ] ์ ๋ณด์๋๊ณผ ํด๋์ค ๋ด๋ถ ๊ตฌ์กฐ (2) | 2026.03.08 |
| 14. [ํ์ด์ฌ] ์คํ์ ๋ฉ์๋์ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ (2) | 2026.03.08 |
| 13. [ํ์ด์ฌ] ์์๊ณผ ํ์ ํ์ธ (0) | 2026.03.08 |