• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

스태틱메소드와 클래스메소드

21.11.17 16:38 작성 조회수 135

2

그 클래스 메소드와 스태틱메소드의 실사용례는 이러할까요?

약간 스태틱메소드는 상속받아도 변하지 않는 것과 관련된 메소드에 사용하고

클래스메소드는 상속받으면 변할 수 있는것과 관련되 메소드에 사용하는?

 

이에 대한 이해는 이부분 을 참고했습니다.

# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.



class wooden_furniture:
    ingredient = "wooden"
    furniture = "furniture"


    @staticmethod
    def get_ingredient():
	    print("made_by_wooden")
	    return "made_by_wooden"

    @classmethod
    def get_furniture(cls):
	    print( f"{cls.furniture}") 
	    return "class"


class wooden_table(wooden_furniture):
    def __init__(self):
        self.furniture = "table"
    


table = wooden_table()
furniture = wooden_furniture()

table.get_ingredient()
table.get_furniture()
furniture.get_ingredient()
furniture.get_furniture()



답변 1

답변을 작성해보세요.

1

안녕하세요! 

네 맞습니다 :) 

사실 스태틱메서드는 클래스메소드로 대체가 가능합니다.

상황에 맞게, 의미에 맞게 유지보수가 쉬운 방향으로 선택해서 사용하시면 됩니다.

파이썬이 제공해주는 하나의 도구라고 생각하세요!