Calling a class method raises a TypeError in Python

前端 未结 8 627
小鲜肉
小鲜肉 2020-12-04 16:02

I don\'t understand how classes are used. The following code gives me an error when I try to use the class.

class MyStuff:
    def average(a, b, c): # Get th         


        
相关标签:
8条回答
  • 2020-12-04 16:37

    You need to spend a little more time on some fundamentals of object-oriented programming.

    This sounds harsh, but it's important.

    • Your class definition is incorrect -- although the syntax happens to be acceptable. The definition is simply wrong.

    • Your use of the class to create an object is entirely missing.

    • Your use of a class to do a calculation is inappropriate. This kind of thing can be done, but it requires the advanced concept of a @staticmehod.

    Since your example code is wrong in so many ways, you can't get a tidy "fix this" answer. There are too many things to fix.

    You'll need to look at better examples of class definitions. It's not clear what source material you're using to learn from, but whatever book you're reading is either wrong or incomplete.

    Please discard whatever book or source you're using and find a better book. Seriously. They've mislead you on how a class definition looks and how it's used.

    You might want to look at http://homepage.mac.com/s_lott/books/nonprog/htmlchunks/pt11.html for a better introduction to classes, objects and Python.

    0 讨论(0)
  • 2020-12-04 16:38

    In python member function of a class need explicit self argument. Same as implicit this pointer in C++. For more details please check out this page.

    0 讨论(0)
  • 2020-12-04 16:38

    You never created an instance.

    You've defined average as an instance method, thus, in order to use average you need to create an instance first.

    0 讨论(0)
  • 2020-12-04 16:42

    To minimally modify your example, you could amend the code to:

    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)
    

    Or to expand it more fully, allowing you to add other methods.

    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()
    
    0 讨论(0)
  • 2020-12-04 16:52

    You can instantiate the class by declaring a variable and calling the class as if it were a function:

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

    However, this won't work with the code you gave us. When you call a class method on a given object (x), it always passes a pointer to the object as the first parameter when it calls the function. So if you run your code right now, you'll see this error message:

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

    To fix this, you'll need to modify the definition of the average method to take four parameters. The first parameter is an object reference, and the remaining 3 parameters would be for the 3 numbers.

    0 讨论(0)
  • 2020-12-04 16:59

    Try this:

    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)
    

    or this:

    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)
    
    0 讨论(0)
提交回复
热议问题