0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python/クラスオブジェクトより値を取得する

Last updated at Posted at 2024-12-09

クラスメソッドの書き方を理解するのに苦労したので備忘録


# -*- 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()


0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?