Calling a class method raises a TypeError in Python

前端 未结 8 653
小鲜肉
小鲜肉 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.

提交回复
热议问题