如何在列表中存储类的实例?

时间:2019-01-10 15:37:01

标签: python class

我正在尝试使用类来创建一个排行榜系统,该系统将按照电影的评分顺序存储电影

我的代码:

class Leaderboard:
    def __init__(self):
        movies = []

class Movie:
    def __init__(self, name, rating=5):
        self.info = name, rating

spiderman = Movie("Spider-Man",7)

我如何将类(spiderman)的实例存储到movies中?

1 个答案:

答案 0 :(得分:3)

除非您将Leaderboard用作静态类(在这种情况下,请不要使用__init__),否则首先需要一个lb = Leaderboard() spiderman = Movie('Spider-Man', 7) lb.movies.append(spiderman) 实例:

#used numbers instead of dates to not have to deal with formatting, but idea is the same.

dt1 <- data.table(id = c('a', 'b', 'c'), date1 = c(1.1, 5.4, 9.1), amt= '100')
dt2 <- data.table(date2 = c(1.3, 3, 6.4, 10.5),
              dt2col = c(1.5, 1.02, 1.005, .99)
              )

dt1[result := prod(dt2[date2-(date1)>0,
                              dt2col
                       ]
     )
     ]