16. [ํŒŒ์ด์ฌ] class methos vs static method

2026. 3. 8. 11:53ยทPython & SQL/Python Basics
๐Ÿ’ก ๋ณธ ํฌ์ŠคํŒ…์€ ์ˆ˜์—… ๋‚ด์šฉ, ๊ต์žฌ, 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
'Python & SQL/Python Basics' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • 18. [ํŒŒ์ด์ฌ] ๋ชจ๋“ˆ๊ณผ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ
  • 17. [ํŒŒ์ด์ฌ] ํด๋กœ์ €์™€ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ
  • 15. [ํŒŒ์ด์ฌ] ์ •๋ณด์€๋‹‰๊ณผ ํด๋ž˜์Šค ๋‚ด๋ถ€ ๊ตฌ์กฐ
  • 14. [ํŒŒ์ด์ฌ] ์ŠคํŽ˜์…œ ๋ฉ”์†Œ๋“œ์™€ ์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋”ฉ
J. Son
J. Son
Petit à petit l'oiseau fait son nid.
  • J. Son
    Steady Study Log
    J. Son
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (170) N
      • Python & SQL (63)
        • Python Basics (21)
        • Python Problems (23)
        • Python Practice (11)
        • MySQL (1)
        • Git & GitHub (7)
      • ML & DL (7) N
      • Projects (5)
        • Project Portfolio (5)
      • AI Camp (4)
        • Camp Reflection (4)
      • Concept Notes (6)
        • Statistics & Stata (4)
        • Mathematics (2)
      • Archive (84)
        • Java (24)
        • R (1)
        • Languages (49)
        • Miscellaneous (10)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

    • GitHub
    • WikiDocs
  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    streamlit
    MySQL
    machine learning
    python streamlit
    GIT
    ๊ฐ์ฒด
    passe compose
    share.streamlit.io
    ๋ณตํ•ฉ๊ณผ๊ฑฐ
    ๋ถˆ์–ด ๊ณต๋ถ€
    ํŒŒ์ด์ฌ ํฌ๋กค๋ง
    Le Petit Prince
    Python
    ํ”„๋กœ์ ํŠธ ์˜ค์ผ๋Ÿฌ
    ํ”„๋ž‘์Šค์–ด ๊ณต๋ถ€
    python problem
    Github
    ์–ด๋ฆฐ์™•์ž ๋ถˆ์–ด
    ๋ถˆ์–ด ๊ด€๊ณ„๋Œ€๋ช…์‚ฌ
    Stata
    ํŒŒ์ด์ฌ streamlit
    ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ 300์ œ
    ํŒŒ์ด์ฌ
    ์ ํ”„ํˆฌํŒŒ์ด์ฌ ์—ฐ์Šต๋ฌธ์ œ ํ’€์ด
    ํŒŒ์ด์ฌ ๋ฌธ์ œ
    ๋จธ์‹ ๋Ÿฌ๋‹
    ์ ํ”„ํˆฌํŒŒ์ด์ฌ ์—ฐ์Šต๋ฌธ์ œ
    ์–ด๋ฆฐ์™•์ž ํ”„๋ž‘์Šค์–ด
    ๋งฅ git
    ์ž๋ฐ”
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.6
J. Son
16. [ํŒŒ์ด์ฌ] class methos vs static method
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”