어떻게 아래의 클래스가 사용하는 변환 할 수 attrs library :파이썬 attrs에 라이브러리 및 참조 예 방법
class MyClass(object):
def __init__(self, api, template=None, **kwargs):
self.api = api
self.param1 = param1
if template is not None:
self.template = api.get_template(desc=template)
구체적으로 어떻게 다른 사람의 초기화에 하나 개의 매개 변수의 의존성을 처리합니까? 내 최초의 시도로,이 같은 클래스로 시작 :
@attr.s
class MyAttrClass(object):
api = attr.ib()
param1 = attr.ib()
하지만 난 template
속성을 처리하는 방법이 확실하지? 내 초기 생각은 다음과 같습니다.
def __attrs_post_init__(self):
self.template = api.get_template(desc=template)
그래도이 작업을 수행하는 더 좋은 방법이 있는지 궁금합니다.