Python类在列表中看不到第二个对象(属性)

时间:2018-10-24 17:24:42

标签: python oop

我想对电影分类类进行编码,该类可以查找(基于某些条件),添加和打印电影。

这是我的代码:

class Movie:
    def __init__(self,name,director,year,location):
        self.name = name
        self.director = director
        self.year = year
        self.location = location
        self.information = {'name':self.name,'director':self.director,'year':self.year,'location':self.location}

    def get_name(self):
        return self.name

    def get_director(self):
        return self.director

    def get_year(self):
        return self.year

    def get_location(self):
        return self.location 

    def get_information(self):
        return self.information

    def __str__(self):
        return f"Name = {self.name},director = {self.director},year = {self.year},location = {self.location}"

class Classification:
    def __init__(self):
        self.movie_list = []

    def length(self):
        return len(self.movie_list)

    def __getitem__(self,key):
        if isinstance(self.movie_list,slice):
            return self.movie_list[key]

    def add_movie(self,movie):
        self.movie_list.append(movie)              

    def print_movie(self):
        for movie in self.movie_list:
            print(movie)

    def find_movie(self,**kwargs):
        check_list = []
        for movie in self.movie_list:
            for name,value in kwargs.items():
                if movie.get_information()[name] == value:
                    check_list.append(True)
                else:
                    check_list.append(False)     
            if all(item == True for item in check_list):
                print(movie)
        check_list.clear()

这里我有一个MovieClassification班; Classification仅具有1个属性,它是电影列表。但是我有两个问题:

a = Movie('Matrix','Dan Yefimov','1999','New York')
b = Movie('Legend','Mak Markus','2005','Kiev')
clasif = Classification()
clasif.add_movie(a)
clasif.add_movie(b)
clasif.find_movie(location = 'New York')
  1. find_movie()适用于列表中的第一部电影(在我们的情况下为a)。但是对于第二个,即使输入正确的搜索参数,它也不会打印任何内容。

  2. 切片不起作用。没有错误信息,它什么也不打印。

您能帮我解决我的问题吗?

P.S我也想听听一些有关改进我的代码的一般建议。

2 个答案:

答案 0 :(得分:2)

通过删除不必要的方法,我们可以大大缩短代码。我们也可以只将__getitem__传递到列表中。我还会在定义__len__方法的过程中使用length

class Movie:
    def __init__(self,name,director,year,location):
        self.name = name
        self.director = director
        self.year = year
        self.location = location
    def __str__(self):
        return f"Name = {self.name},director = {self.director},year = {self.year},location = {self.location}"

class Classification:
    def __init__(self):
        self.movie_list = []    
    def __len__(self):  # __len__ lets you do len(classif)
        return len(self.movie_list)    
    def __getitem__(self,key):
        return self.movie_list[key]    
    def add_movie(self,movie):
        self.movie_list.append(movie)                  
    def print_movie(self):
        for movie in self.movie_list:
            print(movie)       
    def find_movie(self,**kwargs):
        for movie in self.movie_list:
            if all(hasattr(movie, k) and getattr(movie, k) == v for k, v in kwargs.items()):
                print(movie)

答案 1 :(得分:1)

这是您的代码版本,代码会少一些:

class Movie:
    ATTRIBUTES = ('name', 'director', 'year', 'location')

    def __init__(self, name, director, year, location):
        self.name = name
        self.director = director
        self.year = year
        self.location = location

    def __str__(self):
        return ', '.join(
            '{} = {}'.format(attr_name, getattr(self, attr_name))
            for attr_name in self.ATTRIBUTES)


class Classification:
    def __init__(self):
        self.movie_list = []

    def __len__(self):
        return len(self.movie_list)

    def __getitem__(self, key):
        return self.movie_list[key]

    def add_movie(self, movie):
        self.movie_list.append(movie)

    def print_movies(self):
        for movie in self.movie_list:
            print(movie)

    def find_movies(self, **kwargs):
        for movie in self.movie_list:
            do_print = True
            for attr_name, attr_value in kwargs.items():
                if attr_name in Movie.ATTRIBUTES:
                    if getattr(movie, attr_name) != attr_value:
                        do_print = False
            if do_print:
                print(movie)

我在ATTRIBUTES上添加了一个名为Movie的类属性; Movie.__str__()Classification.find_movies()中都使用了它。建议避免在代码中重复很多属性。

在方法Classification.find_movies()中,在将其与电影实例进行比较之前,我检查了它是否为有效属性。无效的参数将被忽略,但是您可以更改代码,以便无效的参数自动导致不打印电影(所有图像将被排除)。