1

I am currently teaching myself about classes but ran into a weird error where I must be doing something basic wrong, it keeps saying gender is not in the argument even though it is under 'male'. My code is below:

class HumanClassification:
    def __init__(self):
        self.age = 0
        self.height = 0
        self.gender = []
    def classification(self, age, height, gender):
        self.age = age
        self.height = height
        self.gender = gender

bob = HumanClassification.classification(32, 6, 'male')
print (bob.age)
Laneciar
  • 83
  • 6
  • 2
    You need an instance of your class. For example, `bob = HumanClassification(); bob.classification(32, 6, 'male')`. You should probably have `age`, `height`, and `gender` be parameters in your `__init__` method. – zondo May 04 '18 at 02:09
  • Thanks man fixed it! – Laneciar May 04 '18 at 02:46

1 Answers1

1

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 classmethods. 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)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • Okay so i fixed the issue with the error but now im stuck on some input, I want it so the user can type the name at the end of the code like 'bob' and it pop up bobs stats(age, pay, gender), and i dont want to use an if statement because that would mean creating a new if statement everytime a new name is created – Laneciar May 04 '18 at 03:05
  • That should probably be a separate question, where you can show your updated code and give more details about what you want. But in general, I'd suggest putting your objects into a dictionary, mapping from name to instance. Variable names should usually not be data. You might also want to add a `__str__` or `__repr__` method to your class, to make it easier to display their data. – Blckknght May 04 '18 at 05:36