クラスメソッドの書き方を理解するのに苦労したので備忘録
# -*- coding: utf-8 -*-
class Test:
def __init__(self, test_id=None, name=None, age=None):
self.test_id = test_id
self.name = name
self.age = age
@classmethod
def test_obj(cls, param):
return cls(
test_id=param.get('test_id'),
name=param.get('name'),
age=param.get('age')
)
class TestAdd:
def add(self):
param = {
'test_id': 1,
'name': 'mike',
'age': 10
}
result = Test.test_obj(param)
return result
class TestResult:
def display(self):
test_add = TestAdd()
result = test_add.add()
print("Test ID: {}, Name: {}, Age: {}".format(result.test_id, result.name, result.age))
# 実行部分
if __name__ == "__main__":
test_result = TestResult()
test_result.display()