Skip to content

Instantly share code, notes, and snippets.

@tkoki
Created October 18, 2021 00:52
Show Gist options
  • Save tkoki/0a3e527e75fdc77aa41d6db0be8051cb to your computer and use it in GitHub Desktop.
Save tkoki/0a3e527e75fdc77aa41d6db0be8051cb to your computer and use it in GitHub Desktop.
Pythonのクラスのプロパティ名の一覧を取得する(セットされているものしか得られない)
# coding: UTF-8
class Test():
def __init__(self, name):
self.name = name
def set_id(self, x):
self.id = x
def who_are_you(self):
print('My name is ' + self.name)
# インスタンス作成
t = Test('marina')
t.who_are_you()
# プロパティの一覧
for key in t.__dict__.keys():
print(key)
# 実行結果(idはセットされていないので現れない)
# My name is marina
# name
# idもセットしてみる
t.set_id(100)
# プロパティの一覧
for key in t.__dict__.keys():
print(key)
# 実行結果(idも出る)
# name
# id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment