I have a question.I'm learning Python and trying to understand superclasses,subclasses and the hierarchy between them.I have one thing that i didn't understand completely. I'm trying to write a program where there's a "Person" superclass and "Birthday" subclass.I defined init method for both of them.What I want to do is, I want to create an instance of Person.At the same time it should create the same instance for Birthday.Because all people have birthdays :) And i want to use the methods that i defined in "Birthday" class. The code;
import datetime
class Person(object):
def __init__(self,name):
self.name=name
Birthday.__init__(self,name)
def gender(self,gender):
self.gender=gender
class Birthday(Person):
def __init__(self,name):
self.objectlike=[]
self.birthday=None
self.youget={}
def setbirth(self,year,month,day):
self.birthday=datetime.date(year,month,day)
def getbirth(self):
return self.birthday
def __str__(self):
return self.name
def setyouget(self,year,thing):
self.youget[year]=thing
def setlikeob(self,thing):
self.objectlike.append(thing)
def nextbirth(self):
thisyearbirth=getbirth().year.replace(year=datetime.date.today().year)
return thisyearbirth
def howlong(self):
return self.nextbirth()-datetime.date.today()
def getage(self):
age=datetime.date.today().year-self.birthday.year
return age
The problem is when i create an instance of Person,for example,
joey=Person('Joey Tribiani')
Then i try to set a birthday for joey;
joey.setbirth(1980,5,5)
It says:'Person' object has no attribute 'setbirth'
How can i overcome this problem? I think i should add some code to init methods but i don't know what to add.I added "Birthday.init(self,name)" to Person class but it didn't work.
(By the way this is my first question.I read many topics on this website.I'm amazed by how helpful the people using it.)