1

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.)

Yasin
  • 23
  • 2
  • 4
  • 3
    Why `Birthday` is a subclass of `Person`? – awesoon Aug 27 '18 at 09:35
  • The class that inherits the other gets the others' method, not the other way around. Also do give [this](https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods?rq=1) a read – Işık Kaplan Aug 27 '18 at 09:35
  • 2
    Also, good Python code doesn't use getters and setters. – khelwood Aug 27 '18 at 09:36
  • 1
    @khelwood can you point me to a resource which says " good Python code doesn't use getters and setters" with implications of why? – Sushant Aug 27 '18 at 09:38
  • 1
    I'd assume they are talking about @property, which actually is a getter and it literally has a setter; so maybe a better phrasing would be python has a better structure for getter and setters. – Işık Kaplan Aug 27 '18 at 09:43
  • @ThatBird I'm talking about `setx`, `getx` methods that you call directly (as opposed to properties). https://www.python-course.eu/python3_properties.php https://docs.quantifiedcode.com/python-anti-patterns/correctness/implementing_java-style_getters_and_setters.html – khelwood Aug 27 '18 at 09:44

2 Answers2

0

As per the code pasted above Person is the Superclass and Birthday is the Subclass. So, It's the Subclass which inherits from Superclass and not the other way round.

Also, Inheritance is an is-a relationship. Have/Has-A relationship corresponds to composition. You can read more here.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
0

There are actually two aspects to inheritance - subtyping (semantical) and implementation inheritance (technical).

Semantically, inheritance describes a "is a" relationship - if B is a subclass of A, then B "is a" A (with some specializations), and you should be able to use instances of A and B just the same way (this is known as the Liskov Substitution Principle).

Technically, inheritance is restricted kind of composition / delegation (which is, semantically, a "has a" relationship) - the subclass has a reference to it's parent class, and methods (and other class attributes) not defined in the subclass are looked up on the parent (and it's parent etc). This means inheritance can also be used for code reuse ("implementation inheritance"), eventually without respect for the liskov substitution principle ("type inheritance")

Note that in dynamically typed languages like Python you don't actually need inheritance for subtyping. You can have a class B that implement the exact same interface as class A - and so is a proper subtype according to Liskov - without inheriting from A at all nor have any common ancestor with A.

Now back to your code... The first obvious error is to make Birthday a subclass of Person, since a birthday is obviously not a person (semantically, the relationship is a composition: a person has a birthday). The second obvious error is a cognitive one: it looks like you get the inheritance relationship the wrong way round. If Birthday inherits from Person then it's the Birthday class that get access to Person methods, not the other way round.

There are other obvious issues, but they are of no real importance ATM. First rethink your code in terms of "has a" (composition / delegation), then it will be time to address those issues.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118