1

so I am trying to understand how to build child classes in python.

So ive created a perent and a child class but I just cant understand how to get them to work

so this is my current code

from abc import ABCMeta, abstractmethod

class Persion(object):
    __metaclass__ = ABCMeta # sets the metaclass to a abstract base class . that means we never call this class directley insted its used for child classes to inhearrit
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def talk(self):
        print("hi my name is " + self.name + " and I am a " + self.gender + ".")

    @abstractmethod
    def PersionType(self):
        """Returns a string of the childs type"""
        pass

class Player(Persion):
    def __int__(self,speed):
        self.name = name
        self.gender = gender
        self.speed = speed
        self.posX = 0
        self.posY = 0

    def moveXY(self, X, Y):
        self.posX = X
        self.posY = Y

    def PersionType(self):
        return 'Player'

player=Persion("Ben","m")
hero = Player(player,30)
hero.moveXY(20,20)
print("you are now at ", hero.posX, "," , hero.posY)
print("your speed is ", hero.speed)
print("your gender is ", hero.gender)
hero.talk()

so the end result is I want these functions to work. but something has gone rong in buildign the inhertance thats what I want to find out.

hero.moveXY(20,20)
print("you are now at ", hero.posX, "," , hero.posY)
print("your speed is ", hero.speed)
print("your gender is ", hero.gender)
hero.talk()

im getting errors sutch as

 line 36, in <module>
    print("your speed is ", hero.speed)
AttributeError: 'Player' object has no attribute 'speed'

I am making this to help me understand how inheratince works in python.

1 Answer 1

2

There are three main problems with your code:

  1. Change __int__ to __init__. Your instance isn't getting initialized, which is why you get the no attribute 'speed' error.

  2. Your subclass (the class that's inheriting, which in this case is Player) should have an __init__ method that takes all the arguments you want associated with it. You can pass the ones used by the superclass (Persion) to the superclass's __init__. That means probably changing the __init__ method to:

    class Player(Persion):
        def __init__(self, name, gender, speed):
            super(Player, self).__init__(name, gender)
            self.speed = speed
            self.posX = 0
            self.posY = 0
    
  3. Now, when creating the instances, you need only create an instance of the subclass. It will inherit the methods from the superclass:

    hero = Player('Ben', 'm', 30)
    

With these changes, these lines:

print("you are now at ", hero.posX, "," , hero.posY)
print("your speed is ", hero.speed)
print("your gender is ", hero.gender)
hero.talk()

Now produce this output:

you are now at  20 , 20
your speed is  30
your gender is  m
hi my name is Ben and I am a m.

Additional small notes:

  • You probably want the class to be named Person instead of Persion.
  • The @abstractmethod method should not be needed.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you you explained that so well. I did not realize it was something as simple as a spelling mistake. So it makes seance but I still don't fully understand that the line super(Player, self).__init__(name, gender) I get the rough idea on what it is doing. setting the values from the supper/parent class but I don't understand clearly what this super() function is doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.