Рекурсивно распечатать все атрибуты, списки, словари и т. д. объекта в Python.Python

Программы на Python
Anonymous
Рекурсивно распечатать все атрибуты, списки, словари и т. д. объекта в Python.

Сообщение Anonymous »

Используя Python 2.7.10, у меня есть этот скрипт:

Код: Выделить всё

#!/usr/bin/python

#Do `sudo pip install boto3` first
import boto3
import json

def generate(key, value):
"""
Creates a nicely formatted Key(Value) item for output
"""
return '{}={}'.format(key, value)

def main():
ec2 = boto3.resource('ec2', region_name="us-west-2")
volumes = ec2.volumes.all()
for vol in volumes:
#print(vol.__str__())
#print(vol.__dict__)
print vol

# vol object has many attributes, which can be another class object.
# For ex:
#vol.volume_id),
#vol.availability_zone),
#vol.volume_type),

# only process when there are tags to process
# HERE: tags is another object which can contain a dict/list
#if vol.tags:
#    for _ in vol.tags:
#        # Get all of the tags
#        output_parts.extend([
#            generate(_.get('Key'), _.get('Value')),
#        ])

# output everything at once.
print ','.join(output_parts)

if __name__ == '__main__':
main()
Существует ли одна функция, которая может рекурсивно печатать все атрибуты объекта с помощью одного вызова? Как я могу напечатать значения val.xxxxx и val.tags.xxxx за один вызов.

Я попробовал распечатать объект, используя .__dict__ или .__str__ () но это не помогло.

Подробнее здесь: https://stackoverflow.com/questions/436 ... -in-python

Вернуться в «Python»