Python Forum
Preserve Encapsulation while Displaying Information
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preserve Encapsulation while Displaying Information
#11
(Dec-07-2017, 04:26 PM)Windspar Wrote: @Mekire @snippsat
It might not be absolute private and name mangling. But docs say to use as such. section 9.6
So it is python way of support private class variables.

That being said, what is the Python way to create a variable and only allow a getter? Or tell the person using my code, this variable after object construction should be read-only.
Reply
#12
If you really want to do that, just use an underscore.  That way it can still be used if they want, but you've gone out of their way to let them know they shouldn't be using it.
class Car:
    def __init__(self, model):
        self._model = model
    def model(self):
        return self._model
Reply
#13
(Dec-07-2017, 04:31 PM)QueenSvetlana Wrote:
(Dec-07-2017, 04:26 PM)Windspar Wrote: @Mekire @snippsat
It might not be absolute private and name mangling. But docs say to use as such. section 9.6
So it is python way of support private class variables.

That being said, what is the Python way to create a variable and only allow a getter? Or tell the person using my code, this variable after object construction should be read-only.

Essentially you create a "getter" without the @property keyword. Suppose I want to keep it pure Python, so my class looks like this:

class Car:
    def __init__(self, ID, name, tire, engine):
        self.ID = ID
        self.name = name
        self.tire = tire
        self.engine = engine
How would I let client code know to use them as read only, if "getters" are not the Python way?
Reply
#14
(Dec-07-2017, 04:48 PM)QueenSvetlana Wrote: How would I let client code know to use them as read only, if "getters" are not the Python way?
You can use @property in a way to get more towards read only,if you really need that.
Maybe not with all variables,let say ID should be more protected.
class Car:
    def __init__(self, ID, name, tire, engine):
        self._ID = ID
        self.name = name
        self.tire = tire
        self.engine = engine

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

    def __str__(self):
        template = "ID: {ID} Name: {name} Tire: {tire} Engine: {engine}"
        return template.format(**vars(self))
Use:
>>> car_1 = Car(100, 'Opel', 'Goodyear', 1.9)
>>> car_1.name
'Opel'
>>> car_1.engine = 3.0
>>> car_1.engine
3.0
>>> car_1.ID = 900
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
AttributeError: can't set attribute
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python-docx: preserve formatting when printing lines Tmagpy 4 2,009 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  Python Encapsulation codinglearner 1 1,249 Mar-24-2022, 07:26 PM
Last Post: deanhystad
  tabula-py, how to preserve a read_pdf() format and export to csv abcoelho 2 3,237 Mar-24-2021, 08:34 PM
Last Post: abcoelho
  Function encapsulation Oldman45 4 2,254 Jan-22-2021, 11:38 AM
Last Post: Oldman45
  How to preserve x-axis labels despite deleted subplot? Mark17 1 1,891 Dec-23-2020, 09:02 PM
Last Post: Mark17
  Preserve xml file format tanffn 3 3,807 Jan-03-2020, 09:35 AM
Last Post: Larz60+
  Pygal: Displaying information for each data point KirkmanJ 0 1,814 Jul-29-2019, 01:10 PM
Last Post: KirkmanJ
  Encapsulation issue iFunKtion 4 3,867 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