简体   繁体   中英

Accessing a list in a Class Object, Python

Would like some direction on how to pull a "bucket" of data from a return object in python. The statuses is a bucket that contains a list of data to display. I've tried a few different ways and can't seem to display anything but the object reference. Basically, what's the best way to display this type of data in this "bucket" or list of data. ":type: list[ObjectedCreatedName]"

fetch = client.fetch('2Oe3UKM_Nt_NG1UG');
print(fetch.statuses)
print(type(fetch.statuses))

Output:
[<ObjectCreatedName object at 0x03CC07F0>]
<class 'list'>

class ObjectCreatedName(object):
     def __init__(self):
        self.code = None
        self.status = None
        self.count = None

You can just:

def print_sequence(sequence):
    seq_type = sequence.__class__.__name__
    elem_type = sequence[0].__class__.__name__ if len(sequence) > 0 else ''
    print('{}[{}]'.format(seq_type, elem_type))

eg:

fetch = client.fetch('2Oe3UKM_Nt_NG1UG');
print_sequence(fetch.statuses)
# list[ObjectCreatedName]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM