为什么 python 在某些情况下打印内存位置而不是 class.name,而在我的代码中则不打印其他位置?



我的代码有一个数学函数,它迭代县类对象列表,并返回具有最大选民投票率的县以及元组中的投票率。

在这里代码:

class County:
def __init__(self, init_name, init_population, init_voters):
self.name = init_name
self.population = init_population
self.voters = init_voters

def highest_turnout(data) :
max_turnout_county = data[0]
max_turnout = (data[0].voters / data[0].population)
for i in range(0,6):
if (data[i].voters / data[i].population) > max_turnout:
max_turnout_county = data[i].name
max_turnout = (data[i].voters / data[i].population)
return (max_turnout_county, max_turnout)

allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  
result = highest_turnout(data) 
print(result) # prints the output of the function

在当前状态下,它将返回所需的输出。 ("切斯特",0.7215045058280377)

但是,如果我更改输出最高的县,例如,如果我将阿勒格尼选民从 645469 更改为 1000480,以便阿勒格尼现在是最大投票率县,则输出将不再按预期返回元组中的县名,而是返回内存位置。

此处输出: (<提交。县对象在>, 0.9999900048976001)

为什么我的代码在第二种情况下输出内存位置而不是第一种情况,我将如何解决这个问题?

你的代码中有错误。 在第一行中,您将整个县对象分配给max_county_variable

max_turnout_county = data[0]

稍后,您仅分配属性名称:

max_turnout_county = data[i].name

要解决此问题,您只需将第一行更改为:

max_turnout_county = data[0].name

您可以使用参数中的County实例初始化max_turnout_county

max_turnout_county = data[0]

这应该初始化为县的名称

max_turnout_county = data[0].name

或者,您可以向County类添加道岔属性:

class County :
def __init__(self, init_name, init_population, init_voters):
self.name = init_name
self.population = init_population
self.voters = init_voters
@property
def turnout(self):
return self.voters / self.population

这大大简化了您的功能:

def highest_turnout(data) :
return max(data, key=lambda c: c.turnout)
# Or, if you really want to return a tuple instead of
# an instance of County,
# c = max(data, key=lambda c: c.turnout)
# return (c.name, c.turnout)

添加这个

def __str__(self):
return self.name

str方法用于显示对象的名称而不是内存位置,它必须是字符串。

控制类的对象在打印时如何显示它们的是__repr__的存在(或者,如果要区分用于调试的内部表示和实际打印出来,则为__str__)方法。

默认__repr__,如果你不在你的类中写一个,是类名和内存位置。

在上面的代码段中,当满足某些条件时,max_turnout_county被分配data[i].name- 即您的实例的名称,这是一个 tring。如果不满足该条件,则同一变量仍与对象本身一起分配data[0]

根据你想对这些对象做什么,最好的办法是为它们编写一个适当的__repr__方法,不要只使用.name,而是在任何赋值中使用实际对象。对象 repr 甚至可以负责输出您关心的任何其他属性,因此无需在函数中保留两个状态变量并返回元组:

class County :
def __init__(self, init_name, init_population, init_voters):
self.name = init_name
self.population = init_population
self.voters = init_voters

# Bonus: yoru object can have a calculated property:
@property
def turnout(self):
return self.voters/self.population

def __repr__(self):
return f"{self.name}, turnout: {(self.turnout * 100):.02f}"
def highest_turnout(data) :
max_turnout_county = data[0]
max_turnout = (data[0].voters / data[0].population)
for i in range(0,6):
if (data[i].turnout) > max_turnout:
max_turnout_county = data[i]    # <- this would change
max_turnout = (data[i].voters / data[i].population)
return max_turnout_county

allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  
result = highest_turnout(data) 
print(result) # prints the output of the function
# as part of the bonus - you don't even need a function for that,
# as each object now "knows" its turnout, Python's built-in 
# "max" function can find your optimal county:
print(max(data, key=lambda c: c.turnout))

最新更新