Python Forum
Preserve Encapsulation while Displaying Information
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preserve Encapsulation while Displaying Information
#3
QueenSvetlana Wrote:I would need to use my getters, would providing a getter for
As mention over Python Is Not Java
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.

Car should usually be first and Tire a subclass.
Example:
class Car:
    def __init__(self, id, name, type):
        self.id = id
        self.name = name
        self.type = type

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

class Tire(Car):
    def __init__(self, id, name, type, tire, engine):
        super().__init__(id, name, type)
        self.tire = tire
        self.engine = engine
Use:
Output:
>>> car = Tire(100, 'Opel', 'Astra', 'Goodyear', 1.9) >>> car.type 'Astra' >>> car.id 100 >>> car.tire 'Goodyear' >>> print(car) ID: 100 Name: Opel Type: Astra Tire: Goodyear Engine: 1.9
Reply


Messages In This Thread
RE: Preserve Encapsulation while Displaying Information - by snippsat - Dec-07-2017, 03:16 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