Created
October 18, 2021 00:52
-
-
Save tkoki/0a3e527e75fdc77aa41d6db0be8051cb to your computer and use it in GitHub Desktop.
Pythonのクラスのプロパティ名の一覧を取得する(セットされているものしか得られない)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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