Python Forum
Preserve Encapsulation while Displaying Information
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preserve Encapsulation while Displaying Information
#1
Suppose I've got the following class to represent a Car and Tire

class Tire:

    def __init__(self, id, name, type):
        self.id = id
        self.name = name
        self.type = type

    @property
    def getID(self):
        return self.id
    
    @property
    def getName(self):
        return self.name

    @property
    def getType(self):
        return self.Type

    def __str__(self):
        return "ID: {0} Name: {1} Type: {2}".format(self.id, self.name, self.type)
class Car:

    def __init__(self, ID, name, tire, engine):
        self.ID = ID
        self.name = name
        self.tire = tire
        self.engine = engine

    @property
    def getID(self):
        return self.ID

    @property
    def getName(self):
        return self.name

    @property
    def getTire(self):
        return self.tire

    @property
    def getEngine(self):
        return self.engine

    def __str__(self):
        return "ID: {0} Name:{1} Tire: {2} Engine: {3}".format(self.ID, self.name, self.tire, self.engine)
Suppose I want to follow OOP principals, given that Tire is an attribute of a Car, how would I return the details of my Tire object?

If I had GUI with fields for each Car attribute, so calling the __str__ is not applicable, I would need to use my getters, would providing a getter for Tire be appropriate or should I expose the tire attributes in Car? or to put it in code:

This:

class Car:

    def __init__(self, ID, name, tire, engine):
        self.ID = ID
        self.name = name
        self.tire = tire
        self.engine = engine

    @property
    def getID(self):
        return self.ID

    @property
    def getName(self):
        return self.name

    @property
    def getTire(self):
        return self.tire

    @property
    def getEngine(self):
        return self.engine
    
    @property
    def getTire(self):
        return self.tire

    def __str__(self):
        return "ID: {0} Name:{1} Tire: {2} Engine: {3}".format(self.ID, self.name, self.tire, self.engine)
Or:

class Car:

    def __init__(self, ID, name, tire, engine):
        self.ID = ID
        self.name = name
        self.tire = tire
        self.engine = engine

    @property
    def getID(self):
        return self.ID

    @property
    def getName(self):
        return self.name

    @property
    def getTire(self):
        return self.tire

    @property
    def getEngine(self):
        return self.engine

    @property
    def getTireID(self):
        return self.tire.getID
    
    @property
    def getTireName(self):
        return self.tire.getName
    
    @property
    def getTireType(self):
        return self.tire.getType

    def __str__(self):
        return "ID: {0} Name:{1} Tire: {2} Engine: {3}".format(self.ID, self.name, self.tire, self.engine)
In the real world, I can open the hood of my car and see all the details I want, the engine, the tires, etc.. I can even get the ID, and Google it if I wanted too. So the second approach seems approiate, but if I update my Tire class with an attribute that the customer/buyer should know about then that means I need to update car as well. I like the first option, but I'm unsure. What is the appropriate(OOP) way to handle this?
Reply


Messages In This Thread
Preserve Encapsulation while Displaying Information - by QueenSvetlana - Dec-07-2017, 02:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Encapsulation codinglearner 2 1,568 Apr-02-2024, 01:26 PM
Last Post: DataScience
  python-docx: preserve formatting when printing lines Tmagpy 4 2,345 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  tabula-py, how to preserve a read_pdf() format and export to csv abcoelho 2 3,494 Mar-24-2021, 08:34 PM
Last Post: abcoelho
  Function encapsulation Oldman45 4 2,398 Jan-22-2021, 11:38 AM
Last Post: Oldman45
  How to preserve x-axis labels despite deleted subplot? Mark17 1 2,060 Dec-23-2020, 09:02 PM
Last Post: Mark17
  Preserve xml file format tanffn 3 4,097 Jan-03-2020, 09:35 AM
Last Post: Larz60+
  Pygal: Displaying information for each data point KirkmanJ 0 1,917 Jul-29-2019, 01:10 PM
Last Post: KirkmanJ
  Encapsulation issue iFunKtion 4 4,075 Mar-07-2017, 10:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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