说实话,我什至不知道怎么称呼这个错误。
def create_vimeo_objects_details_DTO_thumbnail(number=5):
max_widths = []
test_thumbnail = []
for i in range(number):
new_width = randint(0,2540)
max_widths.append(new_width)
dto_thumbnail = {
'Thumbnail' : {
'link' : '',
'width' : new_width
}
}
test_thumbnail = test_thumbnail + [VimeoObjectDetailsDTO(dto_thumbnail).Thumbnail]
print('test thumbnail: ', [a.width for a in test_thumbnail])
dto_thumbnail
是该类的一部分,必须从请求(字典)中启动
class VimeoObjectDetailsDTO:
files = []
pictures = None
duration = None
class FileDto:
width = None
height = None
link = ''
quality = ''
class Pictures:
sizes = []
class Thumbnail:
width = None
link = ''
def __init__(self, response):
if 'files' in response: self.files = response['files']
if 'pictures' in response: self.pictures = response['pictures']
if 'duration' in response: self.duration = response['duration']
if 'FileDto' in response:
if 'width' in response['FileDto']: self.FileDto.width = response['FileDto']['width']
if 'link' in response['FileDto']: self.FileDto.link = response['FileDto']['link']
if 'height' in response['FileDto']: self.FileDto.height = response['FileDto']['height']
if 'quality' in response['FileDto']: self.FileDto.quality = response['FileDto']['quality']
if 'Pictures' in response:
if 'sizes' in response['Pictures']: self.Pictures.sizes = response['Pictures']['sizes']
if 'Thumbnail' in response:
if 'width' in response['Thumbnail']: self.Thumbnail.width = response['Thumbnail']['width']
if 'link' in response['Thumbnail']: self.Thumbnail.link = response['Thumbnail']['link']
执行此功能时,例如:
test thumbnail: [1893, 1893, 1893, 1893, 1893]
但是当我打印max_widths
时,我得到了正确的-不同的数字
如何使该函数返回/正确打印VimeoObjectDetailsDTO.Thumbnails
? (我是否使用“ +”或附加都没关系)
答案 0 :(得分:1)
您只有一个Thumbnail
对象,它是类的所有实例共有的对象,因为它是一个类属性。因此,每当突变self.Thumbnail
时,您都对同一个对象进行操作。
内部类的合法用途并不多,这当然不是合理的。此外,这些类没有任何方法,它们可能是字典,甚至根本不存在,例如Pictures
。
无论如何,无论您做什么,都希望拥有实例属性而不是类属性,因此应在__init__
中创建它们。