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


Messages In This Thread
Is inheritnace the only option - by QueenSvetlana - Oct-29-2017, 10:43 PM
RE: Is inheritnace the only option - by metulburr - Oct-29-2017, 11:50 PM
RE: Is inheritnace the only option - by snippsat - Oct-30-2017, 05:51 PM
RE: Is inheritnace the only option - by metulburr - Oct-30-2017, 10:49 PM
RE: Is inheritnace the only option - by metulburr - Oct-31-2017, 01:41 AM

Forum Jump:

User Panel Messages

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