class Donkey(object):
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return '我是' + self._name
@property
def age(self):
return self._age
d = Donkey('大黑驴', 22)
print(d.name)
print('打印所有属性')
print([key for key, value in Donkey.__dict__.items() if isinstance(value, property)])
我是大黑驴
打印所有属性
[‘name’, ‘age’]
Donkey.__dict_
mappingproxy({‘module’: ‘main’,
‘init’: <function main.Donkey.init(self, name, age)>,
‘name’: <property at 0x2045b545540>,
‘age’: <property at 0x2045b8afb30>,
‘dict’: <attribute ‘dict’ of ‘Donkey’ objects>,
‘weakref’: <attribute ‘weakref’ of ‘Donkey’ objects>,
‘doc’: None})