How do I instantiate this piece of code properly?

39 Views Asked by At

I want to instantiate the two child classes by declaring M,F as variables. From there, I want to print the genders for the resulting instances.

I am following examples closely and I cannot discern why my code doesn't work and I keep getting the error: "get_gender() takes 0 positional arguments but 1 was given"

I would appreciate any advice

class Person:

    class Male(Person):
        
        def get_gender(self):
            print("male")
            

    class Female(Person):
        
        def get_gender(self):
            print("female")
            
M = Male()          # instance
F = Female()        # instance

M.get_gender()
F.get_gender()
1

There are 1 best solutions below

0
Barmar On BEST ANSWER

Male and Female shouldn't be nested inside the parent class Person.

class Person:
    pass

class Male(Person):
    
    def get_gender(self):
        print("male")
        

class Female(Person):
    
    def get_gender(self):
        print("female")
            
M = Male()          # instance
F = Female()        # instance

M.get_gender()
F.get_gender()