Python Forum
Is inheritnace the only option
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is inheritnace the only option
#1
I have a project I was working on in Java, and for fun, I decided to implement it in Python to see the amount of code and result. My requirements are as follows:

We have two types of Students, Domestic and International. Domestic students, don't require documentation, while international students do. I immediately thought of inheritance, and how it might be implemented. This is what I came up with:

Abstract Student base class:

from abc import ABCMeta


class Student(metaclass=ABCMeta):

    def __init__(self, id, firstname, lastname):
        self.__id = id
        self.__firstname = firstname
        self.__lastname = lastname


    @property
    def iden(self):
        return self.__id

    @property
    def first_name(self):
        return self.__firstname

    @property
    def last_name(self):
        return self.__lastname
Domestic:

from Student import Student


class Domestic(Student):

    def __init__(self, iden, firstname, lastname):
        super().__init__(iden, firstname, lastname)

        self.__type_of_student = "Domestic"

    @property
    def student_type(self):
        return self.__type_of_student

    def __str__(self):
        return "ID: " + self.iden + " " + " First: " + self.first_name + " Last Name: " + self.last_name + " " + " Type: " + self.student_type
Inernational:

from Student import Student
from copy import deepcopy


class International(Student):
    def __init__(self, iden, firstname, lastname, docuemnts):
        super().__init__(iden, firstname, lastname)

        self.__documents = deepcopy(docuemnts)
        self.__type_of_student = "International"

    @property
    def international_documents(self):
        return deepcopy(self.__documents)

    def __str__(self):
        return "ID: " + self.iden + " First Name: " + self.first_name + " Lsat Name: " + \
               self.last_name + " Type: " + self.__type_of_student
I was thinking maybe there was another way to solve this without the use of inheritance, I'm just not seeing it. I don't mind if I have to use inheritance, but this is just a class that's a data container with rules. Is there another structure that I can use to implement/enforce those rules?
Reply
#2
inheritance would make sense here.

But a couple things
  1. you can get rid of the private variables as well as the getters and setters. If you need the class variables, just use them.
  2. use format not concatenation 
Quote:return "ID: " + self.iden + " " + " First: " + self.first_name + " Last Name: " + self.last_name + " " + " Type: " + self.student_type
'
return "ID: {} First: {} Last Name: {} Type: {}".format(self.iden, self.first_name, self.last_name, self.student_type)
Recommended Tutorials:
Reply
#3
(Oct-29-2017, 11:50 PM)metulburr Wrote: inheritance would make sense here.

Quote:return "ID: " + self.iden + " " + " First: " + self.first_name + " Last Name: " + self.last_name + " " + " Type: " + self.student_type
'
return "ID: {} First: {} Last Name: {} Type: {}".format(self.iden, self.first_name, self.last_name, self.student_type)

Thanks for the help. Have I implementation my abstract class properly?

Quote:you can get rid of the private variables as well as the getters and setters. If you need the class variables, just use them.

Why not use the getters/setter?
Reply
#4
(Oct-29-2017, 11:53 PM)QueenSvetlana Wrote: Why not use the getters/setter?

Python is not Java(2004)
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans.
Do not write getters and setters.
This is what the 'property' built-in is for.
And do not take that to mean that you should write getters and setters, and then wrap them in 'property'.
That means that until you prove that you need anything more than a simple attribute access,don't write getters and setters.
They are a waste of CPU time, but more important, they are a waste of programmer time.
Not just for the people writing the code and tests, but for the people who have to read and understand them as well.

class Student():
    def __init__(self, id, first_name, last_name, type_of_student):
        self.id = id
        self.first_name = first_name
        self.last_name = last_name
        self.type_of_student = type_of_student

    def international_documents(self, documents):
        return documents
    
    def __str__(self):
        return f'ID: {self.id} First: {self.first_name} Last Name: {self.last_name} Type: {self.type_of_student}'
Use:
>>> Superman = Student(100, 'Clark', 'Kent', 'International')
>>> Superman.id
100
>>> Superman.last_name
'Kent'

>>> print(Superman)
ID: 100 First: Clark Last Name: Kent Type: International

>>> Superman.international_documents('born Kal-El on the planet Krypton')
'born Kal-El on the planet Krypton'
As you see there is no problem to access data attribute directly,no need for getters/setters.
So can have in information doc that International need to apply doc.
Can also make a check that International most apply doc.
Reply
#5
(Oct-30-2017, 05:51 PM)snippsat Wrote: As you see there is no problem to access data attribute directly,no need for getters/setters.
So can have in information doc that International need to apply doc.
Can also make a check that International most apply doc.

Thanks for the help Smile

I wanted it to be an abstract class, because international Students will need documents(passports, drivers license, etc), Domestic students don't require any.

Did I implement the abstract class correctly?
Reply
#6
Quote:I wanted it to be an abstract class, because international Students will need documents(passports, drivers license, etc), Domestic students don't require any.
So dont use the international_documents method on the Students/International class. Just because its there doesnt mean you need to use it.
Recommended Tutorials:
Reply
#7
(Oct-30-2017, 10:49 PM)metulburr Wrote: So dont use the international_documents method on the Students/International class. Just because its there doesnt mean you need to use it.

I think you mean, don't use it on the Domestic class, right?

Why let domestic see documents, and not use it? Why should domestic even be aware of it? Are you saying inheritance is pointless in this sense? Given that I'm using it because of one attribute.
Reply
#8
No i meant International. But you can just abolish inheritance in this example and just use Student for both cases as there isnt enough there.
Recommended Tutorials:
Reply
#9
(Oct-31-2017, 01:41 AM)metulburr Wrote: No i meant International. But you can just abolish inheritance in this example and just use Student for both cases as there isnt enough there.

Thanks for the help! Smile

Just for learning purposes, do I construct and implement an abstruct class properly?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020