Python 在类内使用函数时出错。我能';t将函数分配给新的类方法
我正在尝试为我的类“Cloth”创建一个新方法,该方法称为“max_temp”,为了简化任务,我创建了以下函数(max_temp),以在init部分中保留更干净的代码。 我不明白为什么它不计算“最大温度”方法Python 在类内使用函数时出错。我能';t将函数分配给新的类方法,python,Python,我正在尝试为我的类“Cloth”创建一个新方法,该方法称为“max_temp”,为了简化任务,我创建了以下函数(max_temp),以在init部分中保留更干净的代码。 我不明白为什么它不计算“最大温度”方法 class cloth(): def __init__(self, category, name): self.category = category self.name = name self.max_temp = max_temp
class cloth():
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = max_temperature(category)
def max_temperature(category):
temps = {
'sweater' : 24,
'shirt' : 45}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp)
>>打印(x.max\u温度)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
名称错误:未定义名称“x”
如果在类中定义方法,则需要将其作为第一个参数传递给self
,或者将其定义为staticmethod
或classmethod
。否则,您需要在类之外定义它
因此,max_temperature
需要声明为以下之一:
@staticmethod # or @classmethod (Ill leave looking up the difference to you)
def max_temperature(category):
....
或
或者在类之外定义它
如果在类中定义它,则应通过self
调用它(如果是classmethod,则通过类名):
因此,总体而言:
class cloth:
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = self.max_temperature(category)
@staticmethod
def max_temperature(category): # TODO you should add error handling to this method
temps = {
'sweater' : 24,
'shirt' : 45
}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp) # 24
您是否尝试过使用self.category作为max_temperature函数的参数
class cloth():
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = max_temperature(category)
def max_temperature(self, self.category):
temps = {
'sweater' : 24,
'shirt' : 45}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp)
Austin已经回答了这个问题,但是如果您还不熟悉静态概念,那么您的代码将如下所示
class cloth():
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = self.max_temperature(category)
def max_temperature(self,category):
temps = {'sweater' : 24,'shirt' : 45}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp)
self.max\u temp=self.max\u temperature(category)
并将self
参数添加到max\u temperature
。或者不要将self
添加到参数中,并使您的方法成为@staticmethod
,因为它不使用实例或类的任何属性。这是否合法?为什么要这样做呢?Python的答案是:SyntaxError:invalid syntax
。。。请不要发布根本不起作用的代码。这正是@Error-syntactic reforme在上一次投票的答案中给出的第二个版本。
class cloth:
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = self.max_temperature(category)
@staticmethod
def max_temperature(category): # TODO you should add error handling to this method
temps = {
'sweater' : 24,
'shirt' : 45
}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp) # 24
class cloth():
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = max_temperature(category)
def max_temperature(self, self.category):
temps = {
'sweater' : 24,
'shirt' : 45}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp)
class cloth():
def __init__(self, category, name):
self.category = category
self.name = name
self.max_temp = self.max_temperature(category)
def max_temperature(self,category):
temps = {'sweater' : 24,'shirt' : 45}
return temps[category]
x = cloth('sweater', 'cam_eco')
print(x.max_temp)