콘텐츠로 건너뛰기
Home » class method

class method

[Python] 파이썬의 클래스 메소드, @staticmethod

파이썬의 데코레이터(decorator)문법은 흔히 찾아보기 힘든 문법인데 (거의 프레임워크들에서나 볼 수 있음) 클래스를 만들다보면 요긴하게 쓰이는 부분이 있다. (혹은 책 같은데서 찾아보게 될지도…) class Post: def __init__(self): pass @staticmethod def postFromStructs(structs): if type(structs) == list: result = [] for elem in structs: a = Post() for key in a.keys: setattr(a, key, elem[key]) result.append(a) if len(result) > 1: return result else: return result[0] else: result = Post() for key in structs: setattr(result, key, structs[key]) return result 위 코드에서 @staticmethod라는 데코레이터를 사용했는데, staticmethod는… 더 보기 »[Python] 파이썬의 클래스 메소드, @staticmethod