【发布时间】:2018-08-23 10:21:18
【问题描述】:
class Draw():
'''Class using, for example opengl, to display something on the screen'''
def add(self,size,file_name):
file_name= file_name
size = size
class Image(Draw):
def __init__(self,size,file_name):
self.size = size
self.add(self.size,file_name)
class Gui():
file_names = ['a.jpg','b.jpg']
images = {}
def __init__(self):
for e in self.file_names:
self.image = Image((50,50),file_name=e)
self.images[e] = self.image
def print_size(self):
print(self.image.size)
a = Gui()
a.print_size() #this gives me a proper (50,50) size
for e in a.images.values():
print(e.size) #this gives me wrong size
这是我的代码的简化版本。我没有将对象存储在字典值中的经验。 我的问题是:我没有访问字典中存储对象的正确属性是否正常?在这个示例中,一切正常,但这是编写代码的错误方式吗?
【问题讨论】:
-
这不是问题,但您的
add方法完全没有任何作用。
标签: python object dictionary memory storage