Calling a class method raises a TypeError in Python
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.
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 resultprint mystuff.average(9,18,27)
Please note that an heavy usage of static methods in python is usually a symptom of some bad smell - if you really need functions, then declare them directly on module level.
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 resultmystuff=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 resulta=9b=18c=27mystuff=myclass(a, b, c) print mystuff.average()