The classification
function you've written is an instance method, which means it should be called on an instance of the class. You can do that in two steps:
bob = HumanClassification() # create the instance
bob.classification(32, 6, 'male') # call the method on it
But it might make more sense for the arguments to be passed directly to __init__
, rather than having a separate method. You can provide default arguments if you want it to be possible to create an instance without specifying all the values up front.
class HumanClassification:
def __init__(self, age=0, height=0, gender=''):
self.age = age
self.height = height
self.gender = gender
Using a list as a default argument is usually a bad idea, so I used an empty string as the default for gender
.
Another approach would be to change classification
to a classmethod
, rather than a normal method. A classmethod
is usually called directly on the class, as you're currently doing. Some classes are designed with alternative constructors implemented with classmethod
s. Here's what that might look like:
class HumanClassification:
def __init__(self):
self.age = 0
self.height = 0
self.gender = []
@classmethod # use the classmethod decorator
def classification(cls, age, height, gender): # first arg is the class, not an instance
self = cls() # create a new instance by calling the class
self.age = age
self.height = height
self.gender = gender
return self # return the instance
bob = HumanClassification.classification(32, 6, 'male') # this works now
print (bob.age)