没关系,好像不管 authorLast 是什么,它都不认识;它给出了同样的错误。operator.attrgetter 会不会有问题?
提前致谢。
简短、独立、正确(可编译),示例:
import operator
class Source:
sources_count = 0
list_of_sources = []
def __init__(self, title, author, year, publisher, city_of_publication, summary, type, tags): #basic attributes of Source class with addition to list_of_sources
self.title = title
self.author = author
self.aSplit = author.split()
self.authorFirst = self.aSplit[0]
self.authorLast = self.aSplit[1]
self.year = year
self.publisher = publisher
self.city_of_publication = city_of_publication
self.summary = summary
self.type = type
self.tags = tags
Source.sources_count += 1
Source.list_of_sources.append(self)
s2 = Source("Hi", "Jacob Jenkins", "2013", "Publisher", "City", "Summary", "Print", "this, is, tag")
s1 = Source("Hoop", "Chelsea Chibbles", "2013", "Publisher", "City", "Summary", "Print", "this, is, tag")
print(s2.authorFirst)
print(s2.authorLast)
print(s1.authorFirst)
print(s1.authorLast)
key_last_name = operator.attrgetter("authorLast")
sorted_list = sorted(Source.list_of_sources, key=key_last_name)
print(sorted_list[0].authorLast, sorted_list[1].authorLast)
没有错误。我现在正在检查其余的代码。一旦我取出这 3 个部分(类、方法和函数),它就可以正常工作。也许它确实与酸洗有关。
编辑:问题似乎已经解决了。我的怀疑是我在对属性进行编辑之前对文件进行了腌制,因此对象实际上没有所述属性,因为它们在属性存在之前就被腌制了。它现在工作正常。