Python 获取类对象属性信息
类定义
class IPhone14Pro:
"""iPhone 14 Pro类"""
def __init__(self):
"""对象属性初始化"""
self.name = 'iPhone 14 Pro'
self.height = 5.81
self.width = 2.81
self.depth = 0.31
self.chip = 'A16'
def turn_on(self):
"""开机"""
print(f'{self.name} 开机!')
获取类对象属性信息
# 创建类对象
iPhone14Pro = IPhone14Pro()
# 获取类对象属性信息
iPhone14Pro.__dict__
类对象属性信息:
{
'name': 'iPhone 14 Pro',
'height': 5.81,
'width': 2.81,
'depth': 0.31,
'chip': 'A16'
}