71

我不明白如何使用类。当我尝试使用该类时,以下代码给了我一个错误。

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)

怎么了?

4

8 回答 8

88

您可以通过声明一个变量并像调用函数一样调用该类来实例化该类:

x = mystuff()
print x.average(9,18,27)

但是,这不适用于您提供给我们的代码。当您在给定对象 (x) 上调用类方法时,它总是在调用函数时将指向该对象的指针作为第一个参数传递。因此,如果您现在运行代码,您将看到以下错误消息:

TypeError: average() takes exactly 3 arguments (4 given)

要解决此问题,您需要修改平均方法的定义以采用四个参数。第一个参数是对象引用,其余 3 个参数用于 3 个数字。

于 2008-12-28T23:48:05.033 回答
34

从您的示例中,在我看来您想使用静态方法。

class mystuff:
  @staticmethod
  def average(a,b,c): #get the average of three numbers
    result=a+b+c
    result=result/3
    return result

print mystuff.average(9,18,27)

请注意,在 python 中大量使用静态方法通常是一些难闻的症状 - 如果你真的需要函数,那么直接在模块级别声明它们。

于 2008-12-28T23:51:21.457 回答
13

为了最小化修改您的示例,您可以将代码修改为:

class myclass(object):
        def __init__(self): # this method creates the class object.
                pass

        def average(self,a,b,c): #get the average of three numbers
                result=a+b+c
                result=result/3
                return result


mystuff=myclass()  # by default the __init__ method is then called.      
print mystuff.average(a,b,c)

或者更充分地扩展它,允许您添加其他方法。

class myclass(object):
        def __init__(self,a,b,c):
                self.a=a
                self.b=b
                self.c=c
        def average(self): #get the average of three numbers
                result=self.a+self.b+self.c
                result=result/3
                return result

a=9
b=18
c=27
mystuff=myclass(a, b, c)        
print mystuff.average()
于 2014-05-24T02:51:04.540 回答
4

类中的每个函数,以及每个类变量都必须采用指向的self参数。

class mystuff:
    def average(a,b,c): #get the average of three numbers
            result=a+b+c
            result=result/3
            return result
    def sum(self,a,b):
            return a+b


print mystuff.average(9,18,27) # should raise error
print mystuff.sum(18,27) # should be ok

如果涉及类变量:

 class mystuff:
    def setVariables(self,a,b):
            self.x = a
            self.y = b
            return a+b
    def mult(self):
            return x * y  # This line will raise an error
    def sum(self):
            return self.x + self.y

 print mystuff.setVariables(9,18) # Setting mystuff.x and mystuff.y
 print mystuff.mult() # should raise error
 print mystuff.sum()  # should be ok
于 2008-12-29T03:23:37.220 回答
1

在类的python成员函数中需要显式self参数。this与C++中的隐式指针相同。有关更多详细信息,请查看页面。

于 2008-12-28T23:50:42.953 回答
1

您需要在面向对象编程的一些基础知识上多花一点时间。

这听起来很苛刻,但很重要。

  • 您的类定义不正确——尽管语法恰好是可以接受的。定义是完全错误的。

  • 您完全没有使用该类来创建对象。

  • 您使用类进行计算是不合适的。这种事情是可以做的,但是需要a的高级概念@staticmehod

由于您的示例代码在很多方面都是错误的,因此您无法得到一个整洁的“修复这个”答案。有太多的事情要解决。

您需要查看更好的类定义示例。目前尚不清楚您使用什么源材料来学习,但无论您阅读的书是错误的还是不完整的。

请丢弃您正在使用的任何书籍或资源并找到更好的书籍。严重地。他们在类定义的外观和使用方式上误导了您。

您可能想查看http://homepage.mac.com/s_lott/books/nonprog/htmlchunks/pt11.html以更好地介绍类、对象和 Python。

于 2008-12-29T01:46:17.323 回答
1

试试这个:

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)

或这个:

class mystuff:
    def average(self,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)
于 2017-08-23T11:35:16.347 回答
-2

您从未创建过实例。

您已将平均定义为实例方法,因此,为了使用平均,您需要先创建一个实例。

于 2008-12-29T00:03:20.433 回答