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.