【发布时间】:2021-02-07 15:54:14
【问题描述】:
我创建了一个类,我想从中访问一些值。这是类的样子:
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) :
turnout = (self.voters / self.population) / 100
return #I haven't completed what I need to do
因为我想从函数 1 中访问值,所以我将第二个函数放在该类中以访问这些值。但是,我开始在代码的下方收到 NameError。
result = highest_turnout(data) #this is the name of my function
#this is the error I get
NameError: name 'highest_turnout' is not defined
所以我的问题是,我真的需要将第二个函数放入类中才能访问这些值吗?如果需要,为什么会出现此错误?如何解决?
【问题讨论】:
-
County(....data...).highest_turnout(data)?
标签: python function class nameerror