How to instantiate a class in python

So, I'm trying to learn Python. This seems pretty straightforward, but obviously I don't understand how classes are used. The following code gives me an error when I try to use a class.

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) 

What's wrong?

+49
python
Dec 28 '08 at 23:36
source share
8 answers

You can instantiate a 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 will not work with the code you gave us. When you call a class method for a given object (x), it always passes a pointer to the object as the first parameter when calling the function. Therefore, if you run your code right now, you will see this error message:

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

To fix this, you will need to modify the definition of the middle method to accept four parameters. The first parameter is a reference to the object, and the remaining 3 parameters will be for three numbers.

+59
Dec 28 '08 at 23:48
source share

From your example, it seems to me you want to use a static method.

 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) 

Note that heavy use of static methods in python is usually a symptom of bad smell - if you really need functions, then declare them directly at the module level.

+31
Dec 28 '08 at 23:51
source share

To minimize your example, you can change 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 expand it more fully, allowing you to add other methods.

 #!/usr/bin/env python import sys 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() 
+8
May 24 '14 at 2:51
source share

Each function within the class and each class variable must take a self argument as indicated.

 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 

If class variables are involved:

  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 
+4
Dec 29 '09 at 3:23
source share

The python member function of the class needs an explicit self argument. Same as the implicit this pointer in C ++. For more information, please view this page .

+1
Dec 28 '08 at 23:50
source share

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

It sounds tough, but important.

  • The definition of your class is incorrect - although the syntax is acceptable. The definition is simply incorrect.

  • Your use of the class to create an object is completely absent.

  • Your use of the class to compute is inappropriate. This can be done, but this requires an extended @staticmehod concept.

Since your sample code is not so in many ways, you cannot get a neat "fix" answer. There are too many things to fix.

You will need better examples of class definitions. It is not clear which source material you use to find out, but which book you are reading is incorrect or incomplete.

Please drop any book or source you are using and find the best book. Jokes aside. They mislead you about what a class definition looks like and how it is used.

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

+1
Dec 29 '09 at 1:46
source share

Try the following:

 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 that:

 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
Aug 23 '17 at 11:35 on
source share

You have never created an instance.

You defined the average as an instance method, so to use the average, you need to first create an instance.

-2
Dec 29 '09 at 0:03
source share



All Articles