我将一组MovieFile
对象从 x.py 传递给 y.py 并遍历它们,尝试在 y.py 中使用每个对象的属性
x.py
mfSet = {}
def pop():
## populate mfSet with MovieFile objects
m = MovieFile(title=t, year=y, dir=d, filename=f)
mfSet.setdefault(str(m), []).append(m)
class MovieFile():
def __init__(self, title, dir, filename, year=0):
self.title = title
self.year = year
self.fulltitle = title if year == 0 else title + ' (' + year + ')'
self.dir = dir
self.filename = filename
def __str__(self):
return repr(self.title + ' (' + self.year + ') at ' + self.dir)
y.py
from x import MovieFile, mfSet, pop # not sure if I need to import MovieFile
pop()
for mf in mfSet:
ft = mf.fulltitle # stacktrace says this attr doesn't exist for str object
title = mf.title # printing shows that this is "<built-in method title of str object at 0x10d84a3f0>"
所以我的主要问题是:
fulltitle
为什么 MovieFile 对象被编译为 str 对象,一旦我使用了这些对象,我该如何使用attr?