【发布时间】:2010-09-28 15:39:33
【问题描述】:
我不明白如何使用类。当我尝试使用该类时,以下代码给了我一个错误。
class MyStuff:
def average(a, b, c): # Get the average of three numbers
result = a + b + c
result = result / 3
return result
# Now use the function `average` from the `MyStuff` class
print(MyStuff.average(9, 18, 27))
错误:
File "class.py", line 7, in <module>
print(MyStuff.average(9, 18, 27))
TypeError: unbound method average() must be called with MyStuff instance as first argument (got int instance instead)
怎么了?
【问题讨论】:
-
将课程视为模具。您实际上并不与模具本身进行交互,但您使用模具来创建对象,然后您可以与之交互。为了对对象做一些事情,你必须先创建它。
标签: python python-3.x class methods