Python 将类作为字典中的值
我有一门课:Python 将类作为字典中的值,python,class,dictionary,Python,Class,Dictionary,我有一门课: class Competition: def __init__(self, year = int, distance = int, stages = int, winner = None , cyclist = list): self.year = year self.distance = distance self.stages = stages self.winner = winner s
class Competition:
def __init__(self, year = int, distance = int, stages = int, winner = None , cyclist = list):
self.year = year
self.distance = distance
self.stages = stages
self.winner = winner
self.cyclist = cyclist
def __str__(self):
return "Competition({}, {}, {}, {}, {}".format(self.year, self.distance, self.stages, self.winner, self.cyclist)
def __repr__(self):
return "{} {} {} {} {}".format(self.year, self.distance, self.stages, self.winner, self.cyclist)
我想把这个类作为值和年份的键添加到字典中。
我得到的信息来自一个文件
所以我有这个
with open('tour-de-france.csv') as file:
reader = csv.reader(file)
no_header = next(reader, None)
new_dict = {}
for x in reader:
new_dict[int(x[0])] = Competition(int(x[0]), x[3], x[11], None,[])
print(new_dict)
部分输出是:
{1903: 1903 2428 6 None [], 1904: 1904 2428 6 None [], 1905: 1905 2994 11 None [],
但我希望它是:
{1903: Competition(1903, 2428, 6, None, []), 1904: Competition(1904, 2428, 6, None, []), 1905: Competition(1905, 2994, 11, None, []),
这可能吗?当打印包含类的词典时,它将打印
\uuuu repr\uuu
函数的返回值。因此,您只需按如下方式创建\uuuuuuu repr\uuuuu
函数(因为\uuuuuuu str\uuuuu
函数已经返回您想要的值):
当打印包含类的词典时,它将打印
\uuuu repr\uuu
函数的返回值。因此,您只需按如下方式创建\uuuuuuu repr\uuuuu
函数(因为\uuuuuuu str\uuuuu
函数已经返回您想要的值):
正如@Seth Peace所说,为了获得类的字符串值,Python调用
\uuuu repr\uuu
(如果已定义),而不是\uu str\uu
)
但是,更干净的方法是使用dataclass:
从数据类导入数据类
从键入导入可选
@数据类
班级比赛:
年份:整数
距离:整数
阶段:int
获胜者:可选[str]
骑自行车的人:名单
印刷品(竞赛(2019、3、5、“约翰·多伊”和[]))
通过这样做,
\uuuuuu init\uuuuuu
、\uuuu str\uuuu
和\uuuuu repr\uuuuuu
将已经以一种有意义的方式被定义。正如@Seth Peace所说的,为了获得类的字符串值,Python调用\uuuuu repr\uuu
而不是
但是,更干净的方法是使用dataclass:
从数据类导入数据类
从键入导入可选
@数据类
班级比赛:
年份:整数
距离:整数
阶段:int
获胜者:可选[str]
骑自行车的人:名单
印刷品(竞赛(2019、3、5、“约翰·多伊”和[]))
通过这样做,\uuuuu init\uuuuu
、\uuuu str\uuuu
和\uuuuu repr\uuuuu
将已经以一种有意义的方式进行了定义。打印容器时,它包含其元素的repr。问题是您已经交换了\u str\u
和\u repr\u
方法
尽管如此,如果一个单独的\uuuu str\uuuu
实际上与\uuuu repr\uuuu
完全相同,那么它就没有多大意义,只是写得有点不同。我会添加一些额外的信息,使其对最终用户更有帮助
同样,您在\uuuu init\uuuuu
中混淆了类型注释和默认值。所有将类型作为默认值的类型都不正确。只有winner
是正确的,但是因为它有一个默认值,而其他的没有,所以它需要在最后退出
class Competition:
def __init__(self, year: int, distance: int, stages: int, cyclist: list, winner=None):
self.year = year
self.distance = distance
self.stages = stages
self.cyclist = cyclist
self.winner = winner
def __repr__(self):
s = "Competition({}, {}, {}, {}, {})".format(
self.year,
self.distance,
self.stages,
self.cyclist,
self.winner
)
return s
def __str__(self):
s = "{}: {} km, {} stages, winner {} out of {})".format(
self.year,
self.distance,
self.stages,
self.winner,
self.cyclist,
)
return s
(格式字符串中还缺少右括号。)
然后实例化将如下所示:
year = int(x[0])
new_dict[year] = Competition(year, x[3], x[11], [])
例如:
c = Competition(1903, 2428, 6, [])
print(c) # -> 1903: 2428 km, 6 stages, winner None out of [])
print(repr(c)) # -> Competition(1903, 2428, 6, [], None)
打印容器时,它包含其元素的repr。问题是您已经交换了\u str\u
和\u repr\u
方法
尽管如此,如果一个单独的\uuuu str\uuuu
实际上与\uuuu repr\uuuu
完全相同,那么它就没有多大意义,只是写得有点不同。我会添加一些额外的信息,使其对最终用户更有帮助
同样,您在\uuuu init\uuuuu
中混淆了类型注释和默认值。所有将类型作为默认值的类型都不正确。只有winner
是正确的,但是因为它有一个默认值,而其他的没有,所以它需要在最后退出
class Competition:
def __init__(self, year: int, distance: int, stages: int, cyclist: list, winner=None):
self.year = year
self.distance = distance
self.stages = stages
self.cyclist = cyclist
self.winner = winner
def __repr__(self):
s = "Competition({}, {}, {}, {}, {})".format(
self.year,
self.distance,
self.stages,
self.cyclist,
self.winner
)
return s
def __str__(self):
s = "{}: {} km, {} stages, winner {} out of {})".format(
self.year,
self.distance,
self.stages,
self.winner,
self.cyclist,
)
return s
(格式字符串中还缺少右括号。)
然后实例化将如下所示:
year = int(x[0])
new_dict[year] = Competition(year, x[3], x[11], [])
例如:
c = Competition(1903, 2428, 6, [])
print(c) # -> 1903: 2428 km, 6 stages, winner None out of [])
print(repr(c)) # -> Competition(1903, 2428, 6, [], None)
不能以这种方式将类作为值。当然可以将引用传递给类本身,但是通过这种方式(Competition(1903,2428,6,None,[])
)您实际上想要的是打印一个对象。因此,如果您想打印一个类似于所需输出的字符串,您应该像使用\uuuu str\uuuu
一样,但它将是一个字符串(用引号括起来),而不是声明性类形式。您不能以这种方式将类作为值。当然可以将引用传递给类本身,但是通过这种方式(Competition(1903,2428,6,None,[])
)您实际上想要的是打印一个对象。因此,如果您想打印一个类似于所需输出的字符串,您应该像使用\uuuu str\uuuu
一样,但它将是一个字符串(用引号括起来),而不是声明性类形式。是的,就是这样!谢谢你指出。我在写字典之前做了报告,我忘了。再次感谢您的指点!您也可以在类定义中的def\uu str\uuuu(self):
之后执行\uu repr\uuu=\uuu str\uuuu
。是的,就是这样!谢谢你指出。我在写字典之前做了报告,我忘了。再次感谢您的指点!您也可以在类定义中的\uuu repr\uu=\uuu str\uuu
之后执行\uu repr\uuu=/code>(self):
。不,OP可以使用\uu repr\uuu
来获得他们想要的表示,而不是括在引号中。同样,在本例中,类和实例之间的区别是迂腐的-我们都理解OP的意思。感谢您的评论,@wjandrea,因为\uuuuurepr\uuuuu
而没有完全意识到这一事实。至于对对象和实例的迂腐态度,这绝对不是我的本意,只是因为我看到太多代码错误来自于人们,而他们并没有真正理解这两者之间的区别,所以我每次都试图解释:)哦,我明白了。是的,这很好地指出:)但在我看来,最好将其作为旁白,而不是“不,你不能那样做”。不,OP可以使用\uuuu repr\uuuu
获得他们想要的表示,而不是附上